#
# 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:
  amazonCrypterPassPhrase: '%env(default:amazon_feed_app_secret:AMAZON_FEED_CRYPTER_PASSPHRASE)%'
  amazon_feed_app_secret: '%env(string:APP_SECRET)%'
  amazon_spapi_proxy_host_default: 'https://amazon-xc-proxy.xcart.com'
  amazon_spapi_proxy_host: '%env(default:amazon_spapi_proxy_host_default:string:AMAZON_SPAPI_PROXY_URL)%'

services:
  _defaults:
    autowire: true
    autoconfigure: true

  XC\AmazonSPAPI\Core\Crypter:
    arguments:
      $passPhrase: '%amazonCrypterPassPhrase%'

  XC\AmazonSPAPI\EntityCallback\StatusCallback:
    tags:
      - { name: doctrine.orm.entity_listener, event: prePersist, entity: XC\AmazonSPAPI\Model\FeedSubmission\Status }

  XC\AmazonSPAPI\EntityCallback\FeedSubmissionCallback:
    tags:
      - { name: doctrine.orm.entity_listener, event: prePersist, entity: XC\AmazonSPAPI\Model\FeedSubmission }
      - { name: doctrine.orm.entity_listener, event: postRemove, entity: XC\AmazonSPAPI\Model\FeedSubmission }
      - { name: doctrine.orm.entity_listener, event: preRemove, entity: XC\AmazonSPAPI\Model\FeedSubmission }

  XC\AmazonSPAPI\EntityCallback\CategoryProductsCallback:
    tags:
      - { name: doctrine.orm.entity_listener, event: postPersist, entity: XLite\Model\CategoryProducts }
      - { name: doctrine.orm.entity_listener, event: postUpdate, entity: XLite\Model\CategoryProducts }
      - { name: doctrine.orm.entity_listener, event: postRemove, entity: XLite\Model\CategoryProducts }

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

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

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

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

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

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

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

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

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

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

  XC\AmazonSPAPI\EntityCallback\AmazonMarketplaceCallback:
    tags:
      - { name: doctrine.orm.entity_listener, event: preUpdate, entity: XC\AmazonSPAPI\Model\AmazonMarketplace }
      - { name: doctrine.orm.entity_listener, event: prePersist, entity: XC\AmazonSPAPI\Model\AmazonMarketplace }
      - { name: doctrine.orm.entity_listener, event: postUpdate, entity: XC\AmazonSPAPI\Model\AmazonMarketplace }
      - { name: doctrine.orm.entity_listener, event: postPersist, entity: XC\AmazonSPAPI\Model\AmazonMarketplace }
      - { name: doctrine.orm.entity_listener, event: postLoad, entity: XC\AmazonSPAPI\Model\AmazonMarketplace }

  XC\AmazonSPAPI\Messenger\Handler\FeedSubmissionHandler:
    tags:
      - { name: messenger.message_handler, transport: async_amazon_feeds_commands }

  XC\AmazonSPAPI\Messenger\Handler\AmazonCommandHandler:
    tags:
      - { name: messenger.message_handler, transport: async_amazon_commands }

  XC\AmazonSPAPI\Core\Data\Provider\ANotificationProvider:
    class: XC\AmazonSPAPI\Core\Data\Provider\NotificationProvider
    public: true

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

  XC\AmazonSPAPI\Core\Manager\SystemdServicesManager:
    class: XC\AmazonSPAPI\Core\Manager\SystemdServicesManager
    public: true
    arguments:
      $logger: '@xc.amazon_spapi.logger'

  XC\AmazonSPAPI\Console\Command\ActualizeWorkersState:
    tags: [ 'console.command' ]
    arguments:
      $daemons: !tagged_iterator { tag: 'xcart.amazon.daemons_hook' }

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

      async_amazon_commands:
        dsn: '%env(MESSENGER_TRANSPORT_DSN)%'
        serializer: XCart\Messenger\Serializer
        options:
          queue_name: async_amazon_commands

    routing:
      'XC\AmazonSPAPI\Messenger\Message\FeedSubmissionEnvelope': async_amazon_feeds_commands
      'XC\AmazonSPAPI\Messenger\Message\AmazonCommandEnvelope': async_amazon_commands

x_cart:
  transports_data:
    async_amazon_feeds_commands:
      moduleId: XC-AmazonSPAPI
      description: 'Sends JSON feeds to Amazon for product synchronization. Minimum time between feed submissions is 5 minutes.'
    async_amazon_commands:
      moduleId: XC-AmazonSPAPI
      description: 'Synchronizes product data with Amazon via Listings Item API. During the marketplace categories mapping process, marks products as ready to be sent via JSON feeds API.'
  modules:
    XC-AmazonSPAPI:
      proxyHost: '%amazon_spapi_proxy_host%'
