#
# 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:
  sema.integration_name: !php/const XC\SemaData\IntegrationSettings::SERVICE_NAME

  sema.vehicles_batch_size.default: 10
  sema.vehicles_batch_size: "%env(default:sema.vehicles_batch_size.default:int:SEMA_VEHICLE_BATCH_SIZE)%"

  sema.vehicles_persist_batch_size.default: 10
  sema.vehicles_persist_batch_size: "%env(default:sema.vehicles_persist_batch_size.default:int:SEMA_VEHICLE_PERSIST_BATCH_SIZE)%"

  sema.products_batch_size.default: 10
  sema.products_batch_size: "%env(default:sema.products_batch_size.default:int:SEMA_PRODUCTS_BATCH_SIZE)%"

  sema.file_source_dir.default: !php/const LC_DIR_DATA
  sema.file_source_dir: "%env(default:sema.file_source_dir.default:string:SEMA_FILE_SOURCE_DIR)%"


services:
  _defaults:
    autowire: true

  XC\AutoImportBase\Messenger\Handler\ExecuteCommandHandler:
    tags:
      - {name: messenger.message_handler, transport: async_sema_commands}
      - {name: messenger.message_handler, transport: async_sema_images_commands}
      - {name: messenger.message_handler, transport: async_sema_attachments_commands}
      - {name: messenger.message_handler, transport: async_sema_common_vehicles_commands}

  xc.sema.raw.config.helper:
    class: XC\AutoImportBase\Core\Helper\RawConfigHelper
    arguments:
      $integrationModuleId: 'XC-SemaData'

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

  XC\SemaData\Core\API\Storage\TokenStorage:
    class: XC\SemaData\Core\API\Storage\SemaTokenStorage
    public: true
    arguments:
      $configuration: '@XC\SemaData\Core\Configuration\Configuration'
      $rawConfigHelper: '@xc.sema.raw.config.helper'

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

  xc.sema.api_service:
    class: XC\SemaData\Core\API\APIService
    public: true
    arguments:
      $httpClient: '@xc.sema.symfony.client'
      $communicationLogger: '@xc.sema.communication_logger'

  XC\SemaData\Core\API\TestAPIService:
    decorates: xc.sema.api_service
    arguments:
      - '@.inner'
      - '@=parameter("autoimport.test.api.gateway") ? true : false'

  XC\SemaData\Core\API\APIGatewayVehiclesEmulateBatchDecorator:
    decorates: XC\SemaData\Core\API\APIGateway
    arguments:
      - '@.inner'

  XC\SemaData\Core\API\APIGatewayDecoratorWithFileDataSource:
    decorates: XC\SemaData\Core\API\APIGateway
    arguments:
      $apiGateway: '@.inner'
      $logger: '@xc.sema.logger'

  XC\SemaData\Core\API\AdaptedFileDataSource:
    arguments:
      $logger: '@xc.sema.logger'
    tags:
      - {name: kernel.event_listener, event: sema.brand_import.started, method: onBrandImportStarted}

  XC\SemaData\Core\API\FileDataStorage:
    class: XC\SemaData\Core\API\CacheFileDataStorage

  XC\SemaData\Core\API\CacheFileDataStorage: ~

  XC\SemaData\Core\API\FileDataSource\FileDataSource:
    arguments:
      $logger: '@xc.sema.logger'
      $dataPath: '%sema.file_source_dir%'

  XC\SemaData\Core\API\FileDataSource\FileDataSourceProductConverter: ~
  XC\SemaData\Core\API\FileDataSource\FileDataSourceFitmentConverter: ~

  XC\SemaData\Core\API\APICachedServiceDecorator:
    class: XC\SemaData\Core\API\APICachedServiceDecorator
    arguments:
      $service: '@xc.sema.api_service'
      $configuration: '@XC\SemaData\Core\Configuration\Configuration'

  XC\SemaData\Core\API\APIGateway:
    class: XC\SemaData\Core\API\APIGatewayLogged
    public: true
    arguments:
      $gateway: '@XC\SemaData\Core\API\APIGatewayImplementation'

  XC\SemaData\Core\API\APIGatewayImplementation:
    class: XC\SemaData\Core\API\APIGatewayImplementation
    arguments:
      $apiService: '@=service("XC\\SemaData\\Core\\Configuration\\Configuration").isCached() ? service("XC\\SemaData\\Core\\API\\APICachedServiceDecorator") : service("xc.sema.api_service")'
      $cache: '@XC\AutoImportBase\Core\Cache\Cache'
      $languageRepo: '@autoimport.repository.language'
      $parsingErrors: '@xc.sema.wrongly_parsed_products'
      $logger: '@xc.sema.logger'

  xc.sema.metadata_gateway:
    class: XC\SemaData\Core\Metadata\DBMetadataGateway
    arguments:
      $metadataClassName: '\XC\SemaData\Model\Metadata'
    public: true

  XC\SemaData\Core\API\TrialAPIGateway:
    decorates: XC\SemaData\Core\API\APIGateway
    arguments:
      - '@.inner'
      - '@=parameter("auto_trial.end_date") ? true : false'
      - '%auto_trial.products_limit%'

  XC\SemaData\Core\Data\Updater\CategoryUpdater:
    class: XC\SemaData\Core\Data\Updater\CategoryUpdater
    public: true
    arguments:
      $categoryRepo: '@xcart.repository.category'

  XC\SemaData\Core\Data\Updater\CategoryMapUpdater:
    class: XC\SemaData\Core\Data\Updater\CategoryMapUpdater
    public: true

  XC\SemaData\Core\Data\Updater\ProductFitmentsUpdater:
    class: XC\SemaData\Core\Data\Updater\ProductFitmentsUpdater
    public: true

  XC\SemaData\Core\Data\Updater\ProductUpdater:
    class: XC\SemaData\Core\Data\Updater\ProductUpdater
    public: true
    arguments:
      $helpersWithProductFactory: '@XC\AutoImportBase\Core\Data\Factory\HelpersWithProductFactory'
      $categoryMapRepo: '@xc.sema.repository.category_map'
      $productImageRepo: '@autoimport.repository.product_images'
      $semaMetadataGateway: '@xc.sema.metadata_gateway'
      $updateOptionsHelper: '@xc.sema.product.update.options.helper'

  XC\SemaData\Core\Data\Mapper\CategoryMapper:
    class: XC\SemaData\Core\Data\Mapper\CategoryMapper
    public: true

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

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

  xc.sema.communication_logger:
    class: XC\AutoImportBase\Core\API\CommunicationLogger
    public: true
    arguments:
      $logger: '@xc.sema.logger'
      $httpClient: '@xc.sema.symfony.client'

  xc.sema.mmy_validity_constraint:
    class: XC\AutoImportBase\Core\Command\Constraints\MMYSetupBlockImportConstraint
    public: true
    arguments:
      $vehicles:
        submodel: {make: "test", model: "test", year: 2000, submodel: "test"}

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

  XC\SemaData\Core\EventListener\SemaBrandImportLogsListener:
    tags:
      - {name: kernel.event_listener, event: sema.brand_import.started, method: onStarted}
      - {name: kernel.event_listener, event: auto_import.block.import.constraints.failed, method: onConstraintFailed}
      - {name: kernel.event_listener, event: sema.brand_import.data_received, method: onDataReceived}
      - {name: kernel.event_listener, event: sema.brand_import.data_processed, method: onDataProcessed}
      - {name: kernel.event_listener, event: sema.brand_import.chunks_ready, method: onChunksReady}
      - {name: kernel.event_listener, event: sema.brand_import.chunk_started, method: onChunkStarted}
      - {name: kernel.event_listener, event: sema.brand_import.chunk_processed, method: onChunkProcessed}
      - {name: kernel.event_listener, event: sema.brand_import.exception_thrown, method: onExceptionThrown}

  XC\SemaData\Core\Command\BlockImport\CategoriesImport:
    class: XC\SemaData\Core\Command\BlockImport\CategoriesImportViaPullCategories


  XC\SemaData\Core\Command\BlockImport\ProductsImport:
    class: XC\SemaData\Core\Command\BlockImport\ProductsImportViaPullProducts
    arguments:
      $parsingErrors: '@xc.sema.wrongly_parsed_products'
      $mmyConstraint: '@xc.sema.mmy_validity_constraint'
      $chunkSizeForProcessedPartNumbers: "%sema.vehicles_batch_size%"
      $chunkSizeForFitments: "%sema.vehicles_persist_batch_size%"
      $productUpdateOptionsHelper: '@xc.sema.product.update.options.helper'
      $importProductRepo: '@xc.autoimportbase.repository.import_product'

  XC\SemaData\Core\Data\DTO\VehicleExpander: ~

  XC\SemaData\Core\Data\Corrector\MetadataRemoteProductIdCorrectorFactory:
    class: XC\SemaData\Core\Data\Corrector\MetadataRemoteProductIdCorrectorFactory
    arguments:
      $rawMetadataCollection: '@xc.sema.raw_metadata_collection'

  XC\SemaData\Core\Data\Corrector\MetadataRemoteProductIdCorrector:
    class: XC\SemaData\Core\Data\Corrector\MetadataRemoteProductIdCorrector
    factory: ['@XC\SemaData\Core\Data\Corrector\MetadataRemoteProductIdCorrectorFactory', createCorrector]
    arguments:
      $metadataGateway: '@xc.sema.metadata_gateway'

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

  xc.sema.brand_data_import.inner:
    class: XC\SemaData\Core\Command\BlockImport\SemaBrandImport
    arguments:
      $semaBrandRepo: '@xc.sema.repository.sema_brand'
      $chunkSizeForProducts: '%sema.products_batch_size%'

  xc.sema.brand_data_import.constraint_builder:
    class: XC\SemaData\Core\Command\BlockImport\ConstraintBuilder

  xc.sema.repository.sema_brand:
    class: XC\SemaData\Model\Repo\SemaBrand
    public: true
    factory: ['@doctrine.orm.default_entity_manager', getRepository]
    arguments:
      - XC\SemaData\Model\SemaBrand

  xc.sema.repository.category_map:
    class: XC\AutoImportBase\Model\Repo\AutoCategoryMap
    public: true
    factory: ['@doctrine.orm.default_entity_manager', getRepository]
    arguments:
      - XC\SemaData\Model\CategoryMap

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

  xc.sema.category.map.helper:
    class: XC\AutoImportBase\Core\Data\Helper\CategoryMapHelper
    public: true
    arguments:
      $addedEntityHelper: '@XC\AutoImportBase\Core\Data\Helper\KnownEntityHelper'
      $categoryMapRepo: '@xc.sema.repository.category_map'

  xc.sema.vehicle_data_provider:
    class: XC\SemaData\Core\Provider\SemaDataVehicleDataProvider
    public: true
    arguments:
      $configuration: '@XC\SemaData\Core\Configuration\Configuration'
      $commonVehiclesImportStatusStorage: '@xc.sema.common_vehicle_import_status_storage'

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

  XC\SemaData\IntegrationSettings:
    class: XC\SemaData\IntegrationSettings
    public: true
    tags: [{name: xc.autoimportbase.integration.settings}]
    arguments:
      $productsLimit: '%auto_trial.products_limit%'
      $metadataGateway: '@xc.sema.metadata_gateway'
      $updateOptionsHelper: '@xc.sema.product.update.options.helper'
      $blockRepo: '@xc.sema.repository.brand'
      $blockImport: '@xc.sema.brand_data_import'
      $categoryMapRepo: '@xc.sema.repository.category_map'
      $brandsImportStatusStorage: '@xc.sema.brand_import_status_storage'
      $categoriesImportStatusStorage: '@xc.sema.categories_import_status_storage'
      $topMessageScheduler: '@xc.sema.top_message_scheduler'
      $logger: '@xc.sema.logger'
      $vehicleDataProvider: '@xc.sema.vehicle_data_provider'
      $rawMetadataCollection: '@xc.sema.raw_metadata_collection'

  xc.sema.repository.brand:
    class: XC\SemaData\Model\Repo\SemaBrand
    factory: ['@doctrine.orm.default_entity_manager', getRepository]
    arguments:
      - XC\SemaData\Model\SemaBrand

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

  xc.sema.categories_import_status_storage:
    class: XC\AutoImportBase\Core\Data\Storage\SubBlockImportStatusStorage
    public: true
    arguments:
      $moduleId: 'XC-SemaData'
      $optionName: 'importCategoriesStatus'
      $statusRepo: '@xc.autoimportbase.repository.sub_block_import_status'

  xc.sema.common_vehicle_import_status_storage:
    class: XC\AutoImportBase\Core\Data\Storage\SubBlockImportStatusStorage
    public: true
    arguments:
      $moduleId: 'XC-SemaData'
      $optionName: 'commonVehiclesImportStatus'
      $statusRepo: '@xc.autoimportbase.repository.sub_block_import_status'

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

framework:
  messenger:
    transports:
      async_sema_commands:
        dsn: '%env(MESSENGER_TRANSPORT_DSN)%'
        serializer: XCart\Messenger\Serializer
        options:
          queue_name: sema
      async_sema_images_commands:
        dsn: '%env(MESSENGER_TRANSPORT_DSN)%'
        serializer: XCart\Messenger\Serializer
        options:
          queue_name: sema_images
      async_sema_attachments_commands:
        dsn: '%env(MESSENGER_TRANSPORT_DSN)%'
        serializer: XCart\Messenger\Serializer
        options:
          queue_name: sema_attachments
      async_sema_common_vehicles_commands:
        dsn: '%env(MESSENGER_TRANSPORT_DSN)%'
        serializer:  XCart\Messenger\Serializer
        options:
          queue_name: sema_common_vehicles

    routing:
      'XC\SemaData\Messenger\Message\ExecuteCommand': async_sema_commands
      'XC\SemaData\Messenger\Message\ExecuteImagesCommand': async_sema_images_commands
      'XC\SemaData\Messenger\Message\ExecuteAttachmentsCommand': async_sema_attachments_commands
      'XC\SemaData\Messenger\Message\ExecuteCommonVehiclesCommand': async_sema_common_vehicles_commands

x_cart:
  transports_data:
    async_sema_commands:
      moduleId: XC-SemaData
      description: 'Importing SemaData brands, products, categories.'
    async_sema_images_commands:
      moduleId: XC-SemaData
      description: 'Downloading brands, products, categories images that were imported earlier as links.'
    async_sema_attachments_commands:
      moduleId: XC-SemaData
      description: 'Downloading products attachments that were imported earlier as links.'
    async_sema_common_vehicles_commands:
      moduleId: XC-SemaData
      description: 'Importing vehicles.'
