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

services:
  _defaults:
    autowire: true

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

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

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

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

  xc.asap.auth_service:
    class: XC\ASAP\Core\Auth\BearerAuthService
    arguments:
      $configuration: '@XC\ASAP\Core\Configuration\Configuration'

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

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

  xc.asap.api_service:
    class: XC\ASAP\Core\API\APIService
    public: true
    arguments:
      $httpClient: '@xc.asap.symfony.client'
      $authService: '@xc.asap.auth_service'
      $communicationLogger: '@xc.asap.communication_logger'
  XC\ASAP\Core\API\TestAPIService:
    decorates: xc.asap.api_service
    arguments:
      - '@.inner'
      - '@=parameter("autoimport.test.api.gateway") ? true : false'
  XC\ASAP\Core\API\APIGateway:
    class: XC\ASAP\Core\API\APIGatewayImplementation
    public: true
    arguments:
      $apiService: '@xc.asap.api_service'
      $cache: '@XC\AutoImportBase\Core\Cache\Cache'
      $parser: '@XC\AutoImportBase\Core\Data\DTO\TireSizesParser'
      $dimConverter: '@XC\AutoImportBase\Core\Data\Converter\DimConverter'
      $weightConverter: '@XC\AutoImportBase\Core\Data\Converter\WeightConverter'
      $parsingErrors: '@xc.asap.wrongly_parsed_products'

  XC\ASAP\Core\Data\Helper\ShouldProductUpdateInspectorFactory:
    class: XC\ASAP\Core\Data\Helper\ShouldProductUpdateInspectorFactory
    arguments:
      $rawMetadataCollection: '@xc.asap.raw_metadata_collection'

  XC\ASAP\Core\Data\Helper\ShouldProductUpdateInspector:
    class: XC\ASAP\Core\Data\Helper\ShouldProductUpdateInspector
    factory: ['@XC\ASAP\Core\Data\Helper\ShouldProductUpdateInspectorFactory', create]
    arguments:
      $metadataGateway: '@xc.asap.metadata_gateway'
      $localEntityGateway: '@xc.asap.local_entity_gateway'

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

  XC\ASAP\Core\EventListener\SubBlockImportStatusListener:
    tags:
      - {name: kernel.event_listener, event: auto_import.sub.block.import.status.changed, method: onSubBlockImportStatusChanged}

  XC\ASAP\Core\Data\Updater\CategoryUpdater:
    class: XC\ASAP\Core\Data\Updater\CategoryUpdater
    public: true
    arguments:
      $categoryRepo: '@xcart.repository.category'
      $categoryImageRepo: '@autoimport.repository.category_images'
      $knownEntityHelper: '@XC\AutoImportBase\Core\Data\Helper\KnownEntityHelper'

  XC\ASAP\Core\Data\Updater\ProductUpdater:
    class: XC\ASAP\Core\Data\Updater\ProductUpdater
    public: true
    arguments:
      $mmyConstraint: '@xc.asap.mmy_validity_constraint'
      $helpersWithProductFactory: '@XC\AutoImportBase\Core\Data\Factory\HelpersWithProductFactory'
      $brandRepo: '@qsl.shopbybrand.repository.brand'
      $categoryMapRepo: '@xc.asap.repository.category_map'
      $productImageRepo: '@autoimport.repository.product_images'
      $groupsStorage: '@xc.asap.core.helper.product_groups_storage'
      $updateOptionsHelper: '@xc.asap.product.update.options.helper'

  XC\ASAP\Core\Data\Helper\StaticCallingHelper: ~

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

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

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

  xc.asap.brand_data_import.inner:
    class: XC\ASAP\Core\Command\BlockImport\AsapBrandImport
    arguments:
      $chunkLength: 50
      $metadataGateway: '@xc.asap.metadata_gateway'

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

  XC\ASAP\Core\Command\BlockImport\ProductsImport:
    class: XC\ASAP\Core\Command\BlockImport\ProductsImportViaPullProducts
    arguments:
      $parsingErrors: '@xc.asap.wrongly_parsed_products'

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

  XC\ASAP\Model\Repo\AsapBrand:
    class: XC\ASAP\Model\Repo\AsapBrand
    public: true
    factory: ['@doctrine.orm.default_entity_manager', getRepository]
    arguments:
      - XC\ASAP\Model\AsapBrand

  XC\ASAP\Core\EventListener\AsapBrandImportLogsListener:
    tags:
      - {name: kernel.event_listener, event: asap.brand_import.started, method: onStarted}
      - {name: kernel.event_listener, event: asap.brand_import.data_received, method: onDataReceived}
      - {name: kernel.event_listener, event: asap.brand_import.batch_processed, method: onBatchProcessed}

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

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

  xc.asap.core.helper.product_groups_storage:
    public: true
    class: XC\AutoImportBase\Core\Helper\ProductGroupsStorage
    arguments:
      $metadataGateway: '@xc.asap.metadata_gateway'
      $productsRepo: '@xcart.repository.product.write'
    tags:
      - {name: kernel.event_listener, event: auto_import.products.batch.finished, method: flushGroups}

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

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

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

  xc.asap.repository.brand:
    class: XC\ASAP\Model\Repo\AsapBrand
    factory: ['@doctrine.orm.default_entity_manager', getRepository]
    arguments:
      - XC\ASAP\Model\AsapBrand

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

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

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

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

  XC\ASAP\Core\EventListener\PullVehicleTypesAfterBrandsListener:
    tags:
      - {name: kernel.event_listener, event: auto_import.brands.completed, method: onImportCompleted}

framework:
  messenger:
    transports:
      async_asap_commands:
        dsn: '%env(MESSENGER_TRANSPORT_DSN)%'
        serializer: XCart\Messenger\Serializer
        options:
          queue_name: asap
      async_asap_images_commands:
        dsn: '%env(MESSENGER_TRANSPORT_DSN)%'
        serializer: XCart\Messenger\Serializer
        options:
          queue_name: asap_images

    routing:
      'XC\ASAP\Messenger\Message\ExecuteCommand': async_asap_commands
      'XC\ASAP\Messenger\Message\ExecuteImagesCommand': async_asap_images_commands

x_cart:
  transports_data:
    async_asap_commands:
      moduleId: XC-ASAP
      description: 'Importing ASAP products, categories.'
    async_asap_images_commands:
      moduleId: XC-ASAP
      description: 'Downloading brands, products, categories images that were imported earlier as links.'
