#
# Module services
#
# Copyright (c) 2011-present Qualiteam software Ltd. All rights reserved.
# See https://www.x-cart.com/license-agreement.html for license details.

parameters:
  keystone.integration_name: !php/const XC\Keystone\IntegrationSettings::SERVICE_NAME
  keystone.csv_storage_dir: !php/const LC_DIR_DATA
  keystone.xml_storage_dir: !php/const LC_DIR_DATA

  keystone.xml_ttl.default: 86400
  keystone.xml_ttl: "%env(default:keystone.xml_ttl.default:bool:KEYSTONE_XML_TTL)%"

  keystone.ftp_ttl.default: 86400
  keystone.ftp_ttl: "%env(default:keystone.ftp_ttl.default:bool:KEYSTONE_FTP_TTL)%"

services:
  _defaults:
    autowire: true
    autoconfigure: true

  XC\AutoImportBase\Messenger\Handler\ExecuteCommandHandler:
    tags:
      - {name: messenger.message_handler, transport: async_keystone_commands}

  XC\Keystone\IntegrationSettings:
    class: XC\Keystone\IntegrationSettings
    public: true
    tags: [{name: xc.autoimportbase.integration.settings}]
    arguments:
      $productsLimit: '%auto_trial.products_limit%'
      $metadataGateway: '@xc.keystone.metadata_gateway'
      $updateOptionsHelper: '@xc.keystone.product.update.options.helper'
      $blockRepo: '@xc.keystone.repository.brand'
      $blockImport: '@xc.keystone.brand_data_import'
      $brandsImportStatusStorage: '@xc.keystone.brand_import_status_storage'
      $topMessageScheduler: '@xc.keystone.top_message_scheduler'
      $logger: '@xc.keystone.logger'
      $rawMetadataCollection: '@xc.keystone.raw_metadata_collection'
      $localEntityGateway: '@xc.keystone.local_entity_gateway'

  xc.keystone.repository.brand:
    class: XC\Keystone\Model\Repo\KeystoneBrand
    factory: ['@doctrine.orm.default_entity_manager', getRepository]
    arguments:
      - XC\Keystone\Model\KeystoneBrand

  XC\Keystone\Core\Configuration\Configuration:
    class: XC\Keystone\Core\Configuration\Configuration
    public: true
    arguments:
      $rawConfigHelper: '@xc.keystone.raw.config.helper'
      $currencyRepo: '@xc.autoimportbase.repository.currency'

  xc.keystone.raw.config.helper:
    class: XC\AutoImportBase\Core\Helper\RawConfigHelper
    public: true
    arguments:
      $integrationModuleId: 'XC-Keystone'

  xc.keystone.logger:
    class: Psr\Log\LoggerInterface
    public: true
    factory: ['@XLite\Logger', getLogger]
    arguments:
      - 'XC-Keystone'

  xc.keystone.metadata_gateway:
    class: XC\AutoImportBase\Core\Metadata\DBMetadataGateway
    arguments:
      $metadataClassName: '\XC\Keystone\Model\Metadata'
    public: true

  xc.keystone.product.update.options.helper:
    class: XC\AutoImportBase\Core\Data\Helper\ProductUpdateOptionsHelper
    arguments:
      $configuration: '@XC\Keystone\Core\Configuration\Configuration'
    public: true

  xc.keystone.brand_import_status_storage:
    class: XC\AutoImportBase\Core\Data\Storage\SubBlockImportStatusStorage
    public: true
    arguments:
      $moduleId: 'XC-Keystone'
      $optionName: 'brandsImportStatus'
      $statusRepo: '@xc.autoimportbase.repository.sub_block_import_status'

  xc.keystone.brand_data_import:
    class: XC\AutoImportBase\Core\Command\BlockImport\BlockImportEventsDecorator
    arguments:
      $blockImport: '@xc.keystone.brand_data_import.inner'
      $constraintBuilder: '@xc.keystone.brand_data_import.constraint_builder'
      $integration: '%keystone.integration_name%'
    factory: ['@XC\AutoImportBase\Core\Command\BlockImport\DefaultDecoratedBlockImportFactory', 'create']

  xc.keystone.brand_data_import.inner:
    class: XC\Keystone\Core\Command\BlockImport\KeystoneBrandImport
    arguments:
      $wrongParsedProductsProcessor: '@xc.keystone.wrongly_parsed_products'
      $metadataGateway: '@xc.keystone.metadata_gateway'
      $categoryRepo: '@xcart.repository.category'

  xc.keystone.brand_data_import.constraint_builder:
    class: XC\Keystone\Core\Command\BlockImport\ConstraintBuilder
    arguments:
      $metadataGateway: '@xc.keystone.metadata_gateway'

  xc.keystone.local_entity_gateway:
    class: XC\AutoImportBase\Core\Metadata\LocalEntityGateway
    public: true
    arguments:
      $metadataGateway: '@xc.keystone.metadata_gateway'
      $rawMetadataCollection: '@xc.keystone.raw_metadata_collection'

  xc.keystone.top_message_scheduler:
    class: \XC\AutoImportBase\Core\Scheduler\TopMessageScheduler
    public: true
    arguments:
      $groupName: 'keystoneDataMessages'

  XC\Keystone\Core\API\SoapClientFactory: ~

  XC\Keystone\Core\API\General\GeneralAPIGateway:
    class: XC\Keystone\Core\API\General\GeneralAPIGatewayImplementation
    public: true

  XC\Keystone\Core\Gateway\Brands\BrandsGateway:
    class: XC\Keystone\Core\Gateway\Brands\BrandsGatewayImplementation
    public: true

  XC\Keystone\Core\API\FTP\FTPGateway:
    class: XC\Keystone\Core\API\FTP\FTPGatewayImplementation
    public: true
    arguments:
      $storageDir: '%keystone.csv_storage_dir%'
      $ttl: '%keystone.ftp_ttl%'

  XC\Keystone\Core\API\ElectronicOrder\ElectronicOrderGateway:
    class: XC\Keystone\Core\API\ElectronicOrder\ElectronicOrderGatewayImplementation
    public: true

  XC\Keystone\Core\API\ElectronicOrder\ElectronicDataBuilder:
    class: XC\Keystone\Core\API\ElectronicOrder\ElectronicDataBuilder
    public: true
    arguments:
      $logger: '@xc.keystone.logger'

  XC\Keystone\Core\API\ElectronicOrder\ElectronicDataConverter:
    class: XC\Keystone\Core\API\ElectronicOrder\ElectronicDataConverter
    public: true

  XC\Keystone\Core\API\ElectronicOrder\InventoryXmlHandler:
    class: XC\Keystone\Core\API\ElectronicOrder\InventoryXmlHandler
    arguments:
      $httpClient: '@xc.keystone.symfony.client'
      $storageDir: '%keystone.xml_storage_dir%'
      $ttl: '%keystone.xml_ttl%'

  XC\Keystone\Core\Gateway\ProductData\ProductDataGateway:
    class: XC\Keystone\Core\Gateway\ProductData\ProductDataGatewayImplementation
    public: true
    arguments:
      $logger: '@xc.keystone.logger'

  XC\Keystone\Core\Gateway\OrderProcessing\ShipmentsConverter: ~
  XC\Keystone\Core\Gateway\OrderProcessing\OrderConverter: ~
  XC\Keystone\Core\Gateway\OrderProcessing\WarehouseAddressProvider: ~
  XC\Keystone\Core\Gateway\OrderProcessing\DateFormatter: ~
  XC\Keystone\Core\Gateway\OrderProcessing\DateRangeFormatter: ~

  XC\Keystone\Core\Gateway\OrderProcessing\OrderProcessingGateway:
    class: XC\Keystone\Core\Gateway\OrderProcessing\OrderProcessingGatewayImplementation
    public: true
    arguments:
      $logger: '@xc.keystone.logger'

  XC\Keystone\Core\Gateway\OrderHistory\OrderHistoryGateway:
    class: XC\Keystone\Core\Gateway\OrderHistory\OrderHistoryGatewayImplementation
    public: true

  keystone.xcart.repository.order:
    class: XLite\Model\Repo\Order
    factory: ['@doctrine.orm.default_entity_manager', getRepository]
    arguments:
      - XLite\Model\Order

  auto.xcart.repository.shipping.method:
    class: XLite\Model\Repo\Shipping\Method
    factory: ['@doctrine.orm.default_entity_manager', getRepository]
    arguments:
      - XLite\Model\Shipping\Method

  keystone.xcart.repository.order.status.shipping:
    class: XLite\Model\Repo\Order\Status\Shipping
    factory: ['@doctrine.orm.default_entity_manager', getRepository]
    arguments:
      - XLite\Model\Order\Status\Shipping

  XC\Keystone\Core\Creator\KeystoneOrdersProvider:
    class: XC\Keystone\Core\Creator\KeystoneOrdersProvider
    arguments:
      $metadataGateway: '@xc.keystone.metadata_gateway'
      $shippingMethodRepo: '@auto.xcart.repository.shipping.method'

  XC\Keystone\Core\Creator\KeystoneOrdersCreator:
    class: XC\Keystone\Core\Creator\KeystoneOrdersCreator
    public: true
    arguments:
      $shippingStatusRepo: '@keystone.xcart.repository.order.status.shipping'

  xc.keystone.raw_metadata_collection:
    class: XC\AutoImportBase\Core\Metadata\RawMetadataCollection
    public: true
    arguments:
      $metadataGateway: '@xc.keystone.metadata_gateway'

  XC\Keystone\Core\Data\Factory\KeystoneHelpersWithProductFactory:
    class: XC\Keystone\Core\Data\Factory\KeystoneHelpersWithProductFactory
    arguments:
      $knownEntityHelper: '@XC\AutoImportBase\Core\Data\Helper\KnownEntityHelper'
      $shopByBrandHelper: '@XC\AutoImportBase\Core\Helper\ShopByBrandHelper'
      $makeAdapter: '@XC\AutoImportBase\Core\Data\Helper\MakeAdapter'
      $makeLevelsHelper: '@XC\AutoImportBase\Core\Data\Helper\MakeLevelsHelper'
      $storageHelper: '@autoimport.product.storage.helper'

  XC\Keystone\Core\Data\Updater\ProductUpdater:
    class: XC\Keystone\Core\Data\Updater\ProductUpdater
    public: true
    arguments:
      $helpersWithProductFactory: '@XC\Keystone\Core\Data\Factory\KeystoneHelpersWithProductFactory'
      $updateOptionsHelper: '@xc.keystone.product.update.options.helper'

  XC\Keystone\Core\Data\Updater\ShipmentItemUpdater:
    class: XC\Keystone\Core\Data\Updater\ShipmentItemUpdater
    public: true
    arguments:
      $shippingStatusRepo: '@keystone.xcart.repository.order.status.shipping'

  XC\Keystone\Logic\ShipmentCandidates\KeystoneShipmentsCandidatesProvider:
    public: true
    arguments:
      $metadataGateway: '@xc.keystone.metadata_gateway'
    tags:
      - {name: 'xc.split_shipment.shipment_candidate_provider', priority: 1300}

  XC\Keystone\Core\API\FileHelper: ~

  XC\Keystone\Core\API\ElectronicOrder\HttpClientFactory: ~
  xc.keystone.symfony.client:
    class: Symfony\Component\HttpClient\HttpClient
    arguments:
      $configuration: '@XC\Keystone\Core\Configuration\Configuration'
    factory: ['@XC\Keystone\Core\API\ElectronicOrder\HttpClientFactory', 'createHttpClient']

  xc.keystone.wrongly_parsed_products:
    public: true
    class: XC\AutoImportBase\Core\Data\Helper\WrongParsedProducts

  XC\Keystone\Core\Task\KeystoneOrdersHistoryTask:
    tags:
      - {name: kernel.event_listener, event: xcart.cronjob, method: handleTask}

framework:
  messenger:
    transports:
      async_keystone_commands:
        dsn: '%env(MESSENGER_TRANSPORT_DSN)%'
        serializer: XCart\Messenger\Serializer
        options:
          queue_name: keystone

    routing:
      'XC\Keystone\Messenger\Message\ExecuteCommand': async_keystone_commands
