#
# 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:
  turn14.integration_name: !php/const XC\Turn14\IntegrationSettings::SERVICE_NAME
  turn14_default_ignore_lack_of_categories: false
  turn14.ignore_lack_of_categories: "%env(default:turn14_default_ignore_lack_of_categories:bool:TURN14_IGNORE_LACK_OF_CATEGORIES)%"

services:
  _defaults:
    autowire: true
    autoconfigure: true

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

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

  XC\Turn14\Core\Configuration\Configuration:
    class: XC\Turn14\Core\Configuration\Configuration
    public: true
    arguments:
      $rawConfigHelper: '@xc.turn14.raw.config.helper'
      $currencyRepo: '@xc.autoimportbase.repository.currency'
      $ignoreLackOfCategories: '%turn14.ignore_lack_of_categories%'

  XC\Turn14\Core\API\HttpClientFactory: ~
  xc.turn14.symfony.client:
    class: Symfony\Component\HttpClient\HttpClient
    factory: ['@XC\Turn14\Core\API\HttpClientFactory', 'createHttpClient']

  xc.turn14.api_service:
    class: XC\Turn14\Core\API\APIService
    arguments:
      $httpClient: '@xc.turn14.symfony.client'
      $logger: '@xc.turn14.communication_logger'
      $configuration: '@XC\Turn14\Core\Configuration\Configuration'

  XC\Turn14\Core\API\RateLimiter\RateLimiterConfigurationBuilder: ~

  xc.turn14.rate_limiter_configuration:
    class: XC\AutoImportBase\Core\API\RateLimiter\RateLimiterConfiguration
    public: true
    factory: ['@XC\Turn14\Core\API\RateLimiter\RateLimiterConfigurationBuilder', buildRateLimiterConfiguration]

  xc.turn14.rate_limiter:
    class: XC\AutoImportBase\Core\API\RateLimiter\RateLimiter
    arguments:
      $rateLimiterConfiguration: '@xc.turn14.rate_limiter_configuration'
      $logger: '@xc.turn14.logger'

  XC\Turn14\Core\API\RateLimiter\APIServiceRateLimiter:
    decorates: 'xc.turn14.api_service'
    class: XC\Turn14\Core\API\RateLimiter\APIServiceRateLimiter
    arguments:
      $APIService: '@.inner'
      $rateLimiter: '@xc.turn14.rate_limiter'

  XC\Turn14\Core\API\Storage\TokenStorage:
    class: XC\Turn14\Core\API\Storage\TokenStorageImplementation
    arguments:
      $configuration: '@XC\Turn14\Core\Configuration\Configuration'
      $rawConfigHelper: '@xc.turn14.raw.config.helper'

  XC\Turn14\Core\API\APIGateway:
    class: XC\Turn14\Core\API\APIGatewayImplementation
    public: true
    arguments:
      $apiService: '@xc.turn14.api_service'
      $configuration: '@XC\Turn14\Core\Configuration\Configuration'
      $tokenStorage: '@XC\Turn14\Core\API\Storage\TokenStorage'

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

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

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

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

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

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

  XC\Turn14\Core\Data\Provider\IVehicleProvider:
    class: XC\Turn14\Core\Data\Provider\VehicleLevelsVehicleProvider
    public: true

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

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

  XC\Turn14\Core\Data\Updater\ProductDataUpdater:
    class: XC\Turn14\Core\Data\Updater\ProductDataUpdater
    public: true

  XC\Turn14\Core\Data\Updater\ProductUpdater:
    class: XC\Turn14\Core\Data\Updater\ProductUpdater
    public: true
    arguments:
      $updateOptionsHelper: '@xc.turn14.product.update.options.helper'
      $categoryMapRepo: '@xc.turn14.repository.category_map'

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

  XC\Turn14\Core\Data\Updater\TrackingNumberUpdater:
    class: XC\Turn14\Core\Data\Updater\TrackingNumberUpdater
    public: true
    arguments:
      $shippingStatusRepo: '@auto.xcart.repository.order.status.shipping'

  xc.turn14.shop_by_brand.creator:
    class: XC\AutoImportBase\Core\Data\Creator\ShopByBrandCreator
    public: true

  XC\Turn14\Logic\ShipmentCandidates\Turn14ShipmentsCandidatesProvider:
    public: true
    arguments:
      $metadataGateway: '@xc.turn14.metadata_gateway'
    tags:
      - {name: 'xc.split_shipment.shipment_candidate_provider', priority: 1500}

  XC\Turn14\Core\Data\Identifier\OrderShipmentIdentifier:
    public: true
    arguments:
      $orderDetailRepo: '@auto.xcart.repository.order.details'
      $shipmentRepo: '@split.shipment.xcart.repository.shipment'

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

  auto.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.turn14.brand_data_import:
    class: XC\AutoImportBase\Core\Command\BlockImport\BlockImportEventsDecorator
    arguments:
      $blockImport: '@xc.turn14.brand_data_import.inner'
      $constraintBuilder: '@xc.turn14.brand_data_import.constraint_builder'
      $integration: '%turn14.integration_name%'
    factory: ['@XC\AutoImportBase\Core\Command\BlockImport\DefaultDecoratedBlockImportFactory', 'create']

  xc.turn14.brand_data_import.inner:
    class: XC\Turn14\Core\Command\BlockImport\Turn14BrandImport
    arguments:
      $productDataRepo: '@xc.turn14.repository.product_data'
      $categoryMapRepo: '@xc.turn14.repository.category_map'

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

  xc.turn14.repository.product_data:
    class: XC\Turn14\Model\Repo\ProductData
    public: true
    factory: ['@doctrine.orm.default_entity_manager', getRepository]
    arguments:
      - XC\Turn14\Model\ProductData

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

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

  xc.turn14.repository.brand:
    class: XC\Turn14\Model\Repo\Turn14Brand
    factory: ['@doctrine.orm.default_entity_manager', getRepository]
    arguments:
      - XC\Turn14\Model\Turn14Brand

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

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

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

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

  XC\Turn14\Core\Command\BlockImport\Turn14GenericProductUpdaterConfiguration:
    class: XC\Turn14\Core\Command\BlockImport\Turn14GenericProductUpdaterConfiguration
    public: true
    arguments:
      $productDataRepo: '@xc.turn14.repository.product_data'
      $categoryMapRepo: '@xc.turn14.repository.category_map'

  xc.turn14.pull_locations:
    class: XC\AutoImportBase\Core\Command\Pull\PullLocations
    tags: [{name: xc.autoimportbase.pull_locations}]
    public: true
    arguments:
      $integrationName: '%turn14.integration_name%'
      $logger: '@xc.turn14.logger'
      $locationProvider: '@xc.turn14.location_provider'

  xc.turn14.location_provider:
    class: XC\Turn14\Core\Provider\LocationProvider

framework:
  messenger:
    transports:
      async_turn14_commands:
        dsn: '%env(MESSENGER_TRANSPORT_DSN)%'
        serializer: XCart\Messenger\Serializer
        options:
          queue_name: turn14
      async_turn14_images_commands:
        dsn: '%env(MESSENGER_TRANSPORT_DSN)%'
        serializer: XCart\Messenger\Serializer
        options:
          queue_name: turn14_images

    routing:
      'XC\Turn14\Messenger\Message\ExecuteCommand': async_turn14_commands
      'XC\Turn14\Messenger\Message\ExecuteImagesCommand': async_turn14_images_commands

x_cart:
  transports_data:
    async_turn14_commands:
      moduleId: XC-Turn14
      description: 'Importing Turn14 brands, products, categories.'
    async_turn14_images_commands:
      moduleId: XC-Turn14
      description: 'Downloading brands, products images that were imported earlier as links.'
