#
# 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:
  xcart.cdev.coupons.coupon_types:
    - '%'
    - $
api_platform:
  resource_class_directories:
    - '%kernel.project_dir%/var/run/classes/CDev/Coupons/Model'

dto_generator:
  entities:
    api:
      XCart\API\Entity\Storefront\Cart:
        fields:
          coupons:
            type: XCart\API\Entity\Storefront\Cart\Coupon[]
            groups: [read]

      XCart\API\Entity\Storefront\Cart\Coupon:
        name: Cart coupon
        logicEntity: XCart\Logic\Entity\Storefront\Cart\Coupon
        path: /storefront/carts/{cart_id}/coupons
        register_get_one: true
        register_create_one: true
        register_delete_one: true
        fields:
          code:
            type: string
            annotations:
              Assert\NotBlank: ~
          type:
            type: XCart\Enum\CouponType
            groups: [read]
          value:
            type: float
            groups: [read]

    logic:
      XCart\Logic\Entity\Storefront\Cart:
        fields:
          coupons:
            type: XCart\Logic\Entity\Storefront\Cart\Coupon[]

      XCart\Logic\Entity\Storefront\Cart\Coupon:
        generate_get_one: true
        use_doctrine_get_one: true
        generate_create_one: true
        use_doctrine_create_one: true
        generate_delete_one: true
        use_doctrine_delete_one: true
        doctrineEntity: CDev\Coupons\Model\UsedCoupon
        fields:
          cartId:
            type: string
          code:
            type: string
          type:
            type: string
          value:
            type: float

  transformers:
    - from: XCart\Logic\Entity\Storefront\Cart\NewCoupon
      to: CDev\Coupons\Model\UsedCoupon
    - from: CDev\Coupons\Model\UsedCoupon
      to: XCart\Logic\Entity\Storefront\Cart\NewCoupon
    - from: CDev\Coupons\Model\UsedCoupon
      to: XCart\Logic\Entity\Storefront\Cart\Coupon

  enumerations:
    XCart\Enum\CouponType:
      items:
        Price:
          value: '$'
        Percent:
          value: '%'

services:
  _defaults:
    autowire: true
    autoconfigure: true

  # Validator
  xcart.cdev.coupons.validator.coupon_type:
    class: CDev\Coupons\Validator\Constraint\CouponTypeValidator
    public: true
    arguments:
      $types: '%xcart.cdev.coupons.coupon_types%'

  # API

  # /orders
  CDev\Coupons\API\Endpoint\Order\Transformer\UsedCoupon\OutputTransformerInterface: '@CDev\Coupons\API\Endpoint\Order\Transformer\UsedCoupon\OutputTransformer'
  CDev\Coupons\API\Endpoint\Order\Transformer\UsedCoupon\OutputTransformer: ~

  # /coupons
  CDev\Coupons\API\Endpoint\Coupon\Transformer\OutputTransformerInterface: '@CDev\Coupons\API\Endpoint\Coupon\Transformer\OutputTransformer'
  CDev\Coupons\API\Endpoint\Coupon\Transformer\OutputTransformer: ~
  CDev\Coupons\API\Endpoint\Coupon\Transformer\InputTransformerInterface: '@CDev\Coupons\API\Endpoint\Coupon\Transformer\InputTransformer'
  CDev\Coupons\API\Endpoint\Coupon\Transformer\InputTransformer:
    arguments:
      $couponRepository: '@xcart.repository.cdev.coupons.coupon'
      $productRepository: '@xcart.repository.product'
      $productClassesUpdater: '@xcart.entities.sub_entity_transformer.input.id_collection.product_class'
      $membershipsUpdater: '@xcart.entities.sub_entity_transformer.input.id_bidirectional_collection.cdev.coupons.memberships'
      $zonesUpdater: '@xcart.entities.sub_entity_transformer.input.id_bidirectional_collection.cdev.coupons.zones'
      $productsUpdater: '@xcart.entities.sub_entity_transformer.input.id_collection.coupon_products_by_productid'
      $categoriesUpdater: '@xcart.entities.sub_entity_transformer.input.id_bidirectional_collection.cdev.coupons.categories'

  xcart.entities.sub_entity_transformer.input.id_collection.coupon_products_by_productid:
    class: XLite\API\SubEntityInputTransformer\SubEntityIdCollectionInputTransformer
    arguments:
      $repository: '@xcart.repository.cdev.coupons.coupon_product'
      $name: 'product'

  xcart.entities.id_getter.cdev.coupons.coupon_product:
    class: XLite\API\Helper\IdGetter
    arguments:
      $methodName: 'getProductId'

  xcart.query_builder.factory.used_coupon.read:
    class: XCart\Bundle\DoctrineBridgeBundle\QueryBuilder\Factory\QueryBuilderFactory
    arguments:
      $entityManager: '@doctrine.orm.entity_manager'
      $className: XCart\Bundle\DoctrineBridgeBundle\QueryBuilder\CommonQueryBuilder

  xcart.query_builder.factory.used_coupon.write:
    class: XCart\Bundle\DoctrineBridgeBundle\QueryBuilder\Factory\QueryBuilderFactory
    arguments:
      $entityManager: '@doctrine.orm.entity_manager'
      $className: XCart\Bundle\DoctrineBridgeBundle\QueryBuilder\CommonQueryBuilder

  xcart.repository.cdev.coupons.coupon:
    class: CDev\Coupons\Model\Repo\Coupon
    factory: ['@doctrine.orm.default_entity_manager', getRepository]
    arguments:
      - CDev\Coupons\Model\Coupon

  xcart.repository.cdev.coupons.coupon_product:
    class: CDev\Coupons\Model\Repo\CouponProduct
    factory: ['@doctrine.orm.default_entity_manager', getRepository]
    arguments:
      - CDev\Coupons\Model\CouponProduct

  xcart.repository.usedcoupon.read:
    class: CDev\Coupons\Model\Repo\UsedCoupon
    factory: ['@doctrine.orm.default_entity_manager', getRepository]
    calls:
      - [setQueryBuilderFactory, ['@xcart.query_builder.factory.used_coupon.read']]
    arguments:
      - CDev\Coupons\Model\UsedCoupon

  xcart.repository.usedcoupon.write:
    class: CDev\Coupons\Model\Repo\UsedCoupon
    factory: ['@doctrine.orm.default_entity_manager', getRepository]
    calls:
      - [setQueryBuilderFactory, ['@xcart.query_builder.factory.used_coupon.write']]
    arguments:
      - CDev\Coupons\Model\UsedCoupon

  xcart.data_source.usedcoupon.read:
    class: XCart\Bundle\DoctrineBridgeBundle\DataSource\DoctrineReadDataSource
    arguments:
      $repository: '@xcart.repository.usedcoupon.read'
      $collectionClassName: 'CDev\Coupons\Entity\UsedCouponCollection'

  CDev\Coupons\API\OpenApiTagsRepositoryDecorator:
    decorates: XLite\API\OpenApiTagsRepository

  xcart.entities.sub_entity_transformer.input.id_bidirectional_collection.cdev.coupons.categories:
    class: XLite\API\SubEntityInputTransformer\SubEntityIdBidirectionalCollectionInputTransformer
    arguments:
      $repository: '@xcart.repository.category'
      $name: 'category'
      $idGetter: '@xcart.entities.id_getter.category'
      $revertedGetterName: 'getCoupons'

  xcart.entities.sub_entity_transformer.input.id_bidirectional_collection.cdev.coupons.memberships:
    class: XLite\API\SubEntityInputTransformer\SubEntityIdBidirectionalCollectionInputTransformer
    arguments:
      $repository: '@xcart.repository.membership'
      $name: 'membership'
      $idGetter: '@xcart.entities.id_getter.membership'
      $revertedGetterName: 'getCoupons'

  xcart.entities.sub_entity_transformer.input.id_bidirectional_collection.cdev.coupons.product_classes:
    class: XLite\API\SubEntityInputTransformer\SubEntityIdBidirectionalCollectionInputTransformer
    arguments:
      $repository: '@xcart.repository.product_class'
      $name: 'product class'
      $revertedGetterName: 'getCoupons'

  xcart.entities.sub_entity_transformer.input.id_bidirectional_collection.cdev.coupons.zones:
    class: XLite\API\SubEntityInputTransformer\SubEntityIdBidirectionalCollectionInputTransformer
    arguments:
      $repository: '@xcart.repository.zone'
      $name: 'zone'
      $idGetter: '@xcart.entities.id_getter.zone'
      $revertedGetterName: 'getCoupons'

  CDev\Coupons\EntityCallback\UsedCouponCallback:
    tags:
      - {name: doctrine.orm.entity_listener, event: preRemove, entity: CDev\Coupons\Model\UsedCoupon}

  # Storefront API
  ## cart coupon
  ### common
  CDev\Coupons\API\EventSubscriber\Storefront\CouponAccessValidationEventSubscriber:
    class: CDev\Coupons\API\EventSubscriber\Storefront\CouponAccessValidationEventSubscriber
    arguments:
      $couponRepository: '@xcart.repository.usedcoupon.read'

  ### create one
  CDev\Coupons\API\Endpoint\Storefront\CartCoupon\DataPersister\Persister\Assembler\CreatePayloadAssemblerDecorator:
    class: CDev\Coupons\API\Endpoint\Storefront\CartCoupon\DataPersister\Persister\Assembler\CreatePayloadAssemblerDecorator
    decorates: xcart.api.storefront.cart.coupon.data_persister.persister.request.assembler.create
    arguments:
      $inner: '@.inner'

  CDev\Coupons\API\Endpoint\Storefront\CartCoupon\SubIriConverter\SubIriConverter:
    class: CDev\Coupons\API\Endpoint\Storefront\CartCoupon\SubIriConverter\SubIriConverter
    tags:
      - xcart.api.sub_iri_from_item_converter
    arguments:
      $usedCouponReadRepository: '@xcart.repository.usedcoupon.read'

  CDev\Coupons\Logic\Action\Storefront\CartCoupon\CreateOne\Assembler\CalculationResponseAssemblerDecorator:
    decorates: xcart.logic.storefront.cart.coupon.create_one.response.assembler
    decoration_priority: 100
    arguments:
      $inner: '@.inner'
      $writeRepository: '@xcart.repository.cart.write'

  ### delete one
  CDev\Coupons\Logic\Action\Storefront\CartCoupon\DeleteOne\Assembler\CalculationResponseAssemblerDecorator:
    decorates: xcart.logic.storefront.cart.coupon.delete_one.response.assembler
    decoration_priority: 100
    arguments:
      $inner: '@.inner'
      $writeRepository: '@xcart.repository.cart.write'

  ## Transformers
  CDev\Coupons\Transformer\XLite\Model\OrderItem\Logic\Entity\Storefront\CartDecorator:
    class: CDev\Coupons\Transformer\XLite\Model\Cart\Logic\Entity\Storefront\CartDecorator
    decorates: XCart\Transformer\XLite\Model\Cart\Logic\Entity\Storefront\Cart
    decoration_priority: 50

  CDev\Coupons\Transformer\XLite\Model\OrderItem\Logic\Entity\Storefront\NewCartDecorator:
    class: CDev\Coupons\Transformer\XLite\Model\Cart\Logic\Entity\Storefront\NewCartDecorator
    decorates: XCart\Transformer\XLite\Model\Cart\Logic\Entity\Storefront\NewCart
    decoration_priority: 50

  CDev\Coupons\Transformer\Logic\Entity\Storefront\Cart\NewCoupon\CDev\Coupons\Model\UsedCouponDecorator:
    class: CDev\Coupons\Transformer\Logic\Entity\Storefront\Cart\NewCoupon\CDev\Coupons\Model\UsedCouponDecorator
    decorates: XCart\Transformer\Logic\Entity\Storefront\Cart\NewCoupon\CDev\Coupons\Model\UsedCoupon
    decoration_priority: 100
    arguments:
      $inner: '@.inner'
      $couponWriteRepository: '@xcart.repository.cdev.coupons.coupon'
      $cartWriteRepository: '@xcart.repository.cart.write'

  CDev\Coupons\Transformer\CDev\Coupons\Model\UsedCoupon\Logic\Entity\Storefront\Cart\NewCouponDecorator:
    class: CDev\Coupons\Transformer\CDev\Coupons\Model\UsedCoupon\Logic\Entity\Storefront\Cart\NewCouponDecorator
    decorates: XCart\Transformer\CDev\Coupons\Model\UsedCoupon\Logic\Entity\Storefront\Cart\NewCoupon
    decoration_priority: 100

  CDev\Coupons\Transformer\CDev\Coupons\Model\UsedCoupon\Logic\Entity\Storefront\Cart\CouponDecorator:
    class: CDev\Coupons\Transformer\CDev\Coupons\Model\UsedCoupon\Logic\Entity\Storefront\Cart\CouponDecorator
    decorates: XCart\Transformer\CDev\Coupons\Model\UsedCoupon\Logic\Entity\Storefront\Cart\Coupon
    decoration_priority: 100

  CDev\Coupons\Operation\Service\API\GetPathsWithCartIdDecorator:
    class: CDev\Coupons\Operation\Service\API\GetPathsWithCartIdDecorator
    decorates: XCart\Operation\Service\API\GetPathsWithCartId
    decoration_priority: 100
