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

services:
  _defaults:
    autowire: true

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

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

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

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

  xc.autosync.auth_service:
    class: XC\AutoSync\Core\Auth\AuthService
    arguments:
      $configuration: '@XC\AutoSync\Core\Configuration\Configuration'

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

  xc.autosync.api_service:
    class: XC\AutoSync\Core\API\APIService
    public: true
    arguments:
      $httpClient: '@xc.autosync.symfony.client'
      $authService: '@xc.autosync.auth_service'
      $communicationLogger: '@xc.autosync.communication_logger'

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

  XC\AutoSync\Core\API\APIGateway:
    class: XC\AutoSync\Core\API\APIGatewayImplementation
    public: true
    arguments:
      $apiService: '@xc.autosync.api_service'
      $parsingErrors: '@xc.autosync.wrongly_parsed_products'
      $weightConverter: '@XC\AutoImportBase\Core\Data\Converter\WeightConverter'

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

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

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

  xc.autosync.brand_data_import.inner:
    class: XC\AutoSync\Core\Command\BlockImport\AutoSyncBrandImport
    arguments:
      $metadataGateway: '@xc.autosync.metadata_gateway'

  xc.autosync.brand_data_import.constraint_builder:
    class: XC\AutoSync\Core\Command\BlockImport\ConstraintBuilder

  XC\AutoSync\Core\Command\BlockImport\ProductsImport:
    class: XC\AutoSync\Core\Command\BlockImport\ProductsImportViaPullCommands
    arguments:
      $parsingErrors: '@xc.autosync.wrongly_parsed_products'
      $rawMetadataCollection: '@xc.autosync.raw_metadata_collection'

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

  XC\AutoSync\Core\EventListener\AutoSyncBrandImportLogsListener:
    tags:
      - {name: kernel.event_listener, event: autosync.brand_import.started}
      - {name: kernel.event_listener, event: autosync.brand_import.pull_wheels}
      - {name: kernel.event_listener, event: autosync.brand_import.pull_tires}
    arguments:
      $logger: '@xc.autosync.logger'

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

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

  xc.autosync.realtime_import_vehicle_images:
    class: XC\AutoImportBase\Core\EventListener\RealtimeImportVehicleImages
    tags:
      - {name: kernel.event_listener, event: auto_import.vehicle.make.logo.before, method: onBeforeVehicleUpdate}
      - {name: kernel.event_listener, event: auto_import.vehicle.make.logo.after, method: onAfterVehicleUpdate}
      - {name: kernel.event_listener, event: auto_import.vehicle.make.logo.batch_finished, method: onVehicleBatchFinished}


  XC\AutoSync\Core\Data\Updater\ProductUpdaterPriceHelper: ~

  XC\AutoSync\Core\Data\Updater\TireUpdater:
    class: XC\AutoSync\Core\Data\Updater\TireUpdater
    public: true
    arguments:
      $helpersWithProductFactory: '@XC\AutoImportBase\Core\Data\Factory\HelpersWithProductFactory'
      $metadataGateway: '@xc.autosync.metadata_gateway'
      $updateOptionsHelper: '@xc.autosync.product.update.options.helper'
      $groupsStorage: '@xc.autosync.core.helper.product_groups_storage'
      $categoryMapRepo: '@xc.autosync.repository.category_map'

  XC\AutoSync\Core\Data\Updater\WheelUpdater:
    class: XC\AutoSync\Core\Data\Updater\WheelUpdater
    public: true
    arguments:
      $helpersWithProductFactory: '@XC\AutoImportBase\Core\Data\Factory\HelpersWithProductFactory'
      $metadataGateway: '@xc.autosync.metadata_gateway'
      $updateOptionsHelper: '@xc.autosync.product.update.options.helper'
      $groupsStorage: '@xc.autosync.core.helper.product_groups_storage'
      $categoryMapRepo: '@xc.autosync.repository.category_map'

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

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

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

  xc.autosync.repository.metadata:
    class: XC\AutoSync\Model\Repo\Metadata
    factory: ['@doctrine.orm.default_entity_manager', getRepository]
    arguments:
      - XC\AutoSync\Model\Metadata

  xc.autosync.vehicle_data_provider:
    class: XC\AutoSync\Core\Provider\AutoSyncVehicleDataProvider
    public: true
    arguments:
      $configuration: '@XC\AutoSync\Core\Configuration\Configuration'
      $vehicleImportStatusStorage: '@xc.autosync.vehicle_import_status_storage'

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

  xc.autosync.repository.brand:
    class: XC\AutoSync\Model\Repo\AutoSyncBrand
    factory: ['@doctrine.orm.default_entity_manager', getRepository]
    arguments:
      - XC\AutoSync\Model\AutoSyncBrand


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

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

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

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

  xc.autosync.vehicle_import_status_storage:
    class: XC\AutoImportBase\Core\Data\Storage\SubBlockImportStatusStorage
    public: true
    arguments:
      $moduleId: 'XC-AutoSync'
      $optionName: 'importVehiclesStatus'
      $statusRepo: '@xc.autoimportbase.repository.sub_block_import_status'

  XC\AutoSync\Core\Data\Updater\CategoryMapUpdater:
    public: true
    arguments:
      $categoryMapHelper: '@xc.autosync.category.map.helper'
      $categoryMapUpdater: '@XC\AutoImportBase\Core\Data\Updater\CategoryMapUpdater'

  XC\AutoSync\Core\Data\Updater\CategoryMappingFlattenedStorage:
    public: true

  XC\AutoSync\Core\Data\Updater\MakeImageUpdater:
    public: true
    arguments:
      $storageHelper: '@autoimport.brand.storage.helper'

  XC\AutoSync\Core\Data\Updater\ModelLogoUpdater:
    public: true
    arguments:
      $storageHelper: '@autoimport.brand.storage.helper'

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

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

  XC\AutoSync\Core\Manager\EntityIdManager:
    class: XC\AutoSync\Core\Manager\EntityIdManager
    public: true

  XC\AutoSync\Core\Command\BlockImport\AutoSyncGenericProductUpdaterConfiguration: ~

  XC\AutoSync\Core\Data\Updater\SubmodelLogoUpdater:
    class: XC\AutoSync\Core\Data\Updater\SubmodelLogoUpdater
    public: true
    arguments:
      $configuration: '@XC\AutoSync\Core\Configuration\Configuration'
      $storageHelper: '@autoimport.product.storage.helper'

framework:
  messenger:
    transports:
      async_autosync_commands:
        dsn: '%env(MESSENGER_TRANSPORT_DSN)%'
        serializer: XCart\Messenger\Serializer
        options:
          queue_name: autosync
      async_autosync_images_commands:
        dsn: '%env(MESSENGER_TRANSPORT_DSN)%'
        serializer: XCart\Messenger\Serializer
        options:
          queue_name: autosync_images
      async_autosync_vehicles_commands:
        dsn: '%env(MESSENGER_TRANSPORT_DSN)%'
        serializer: XCart\Messenger\Serializer
        options:
          queue_name: autosync_vehicles

    routing:
      'XC\AutoSync\Messenger\Message\ExecuteCommand': async_autosync_commands
      'XC\AutoSync\Messenger\Message\ExecuteImagesCommand': async_autosync_images_commands
      'XC\AutoSync\Messenger\Message\ExecuteVehiclesCommand': async_autosync_vehicles_commands

x_cart:
  transports_data:
    async_autosync_commands:
      moduleId: XC-AutoSync
      description: 'Importing AutoSync brands, products.'
    async_autosync_images_commands:
      moduleId: XC-AutoSync
      description: 'Downloading brands and products images that were imported earlier as links.'
    async_autosync_vehicles_commands:
      moduleId: XC-AutoSync
      description: 'Importing vehicles with Wheel/Tire options.'
