#
# Module services
#
# Copyright (c) 2011-present Qualiteam software Ltd. All rights reserved.
# See https://www.x-cart.com/license-agreement.html for license details.

api_platform:
  resource_class_directories:
    - '%kernel.project_dir%/var/run/classes/XC/ProductVariants/Model'

dto_generator:
  entities:
    api:
      XCart\API\Entity\Storefront\Product\Detailed:
        fields:
          variants:
            type: XCart\API\Entity\Storefront\ProductVariant[]

      XCart\API\Entity\Storefront\Cart\Item:
        fields:
          variant:
            type: XCart\API\Entity\Storefront\ProductVariant
            nullable: true
            groups: [read]

    logic:
      XCart\Logic\Entity\Storefront\Product:
        fields:
          variants:
            type: XCart\Logic\Entity\Storefront\ProductVariant[]

      XCart\Logic\Entity\Storefront\Cart\Item:
        fields:
          variant:
            type: XCart\Logic\Entity\Storefront\ProductVariant
            nullable: true

    dto:
      XCart\API\Entity\Storefront\ProductVariant:
        fields:
          id:
            type: integer
          sku:
            type: string
          price:
            type: float
            nullable: true
          amount:
            type: integer
          weight:
            type: float
          image:
            type: XCart\API\Entity\Storefront\Image
            nullable: true
          attributes:
            type: XCart\API\Entity\Storefront\VariantAttribute[]
          isDefault:
            type: bool

      XCart\Logic\Entity\Storefront\ProductVariant:
        fields:
          id:
            type: integer
          sku:
            type: string
          price:
            type: float
            nullable: true
          amount:
            type: integer
          weight:
            type: float
          image:
            type: XCart\Logic\Entity\Storefront\Image
            nullable: true
          attributes:
            type: XCart\Logic\Entity\Storefront\VariantAttribute[]
          isDefault:
            type: bool

      XCart\API\Entity\Storefront\VariantAttribute:
        fields:
          id:
            type: integer
          name:
            type: string
          value:
            type: string

      XCart\Logic\Entity\Storefront\VariantAttribute:
        fields:
          id:
            type: integer
          name:
            type: string
          value:
            type: string

services:
  _defaults:
    autowire: true
    autoconfigure: true
    public: true

  # /product/{product_id}/variants
  XC\ProductVariants\API\Endpoint\ProductVariant\Transformer\OutputTransformerInterface: '@XC\ProductVariants\API\Endpoint\ProductVariant\Transformer\OutputTransformer'
  XC\ProductVariants\API\Endpoint\ProductVariant\Transformer\OutputTransformer: ~
  XC\ProductVariants\API\Endpoint\ProductVariant\Transformer\InputTransformerInterface: '@XC\ProductVariants\API\Endpoint\ProductVariant\Transformer\InputTransformer'
  XC\ProductVariants\API\Endpoint\ProductVariant\Transformer\InputTransformer: ~

  XC\ProductVariants\API\Endpoint\ProductVariant\SubIriConverter\SubIriConverter:
    tags: [!php/const XCart\Framework\ApiPlatform\Core\Bridge\Symfony\Routing\SubIriConverter\SubIriFromItemConverterInterface::SUB_IRI_FROM_ITEM_CONVERTER_TAG]

  XC\ProductVariants\API\Endpoint\ProductVariant\DataProvider\CollectionDataProvider:
    arguments:
      $inner: '@api_platform.doctrine.orm.default.collection_data_provider'
      $repository: '@xcart.repository.product'

  XC\ProductVariants\API\Endpoint\ProductVariant\DataProvider\ItemDataProvider:
    arguments:
      $inner: '@api_platform.doctrine.orm.default.item_data_provider'
      $repository: '@xcart.repository.product'

  xcart.api.xc.product_variants.product_variant.controller:
    class: XC\ProductVariants\Controller\API\ProductVariant\Post
    arguments:
      $repository: '@xcart.repository.product'

  XC\ProductVariants\API\OpenApiTagsRepositoryDecorator:
    decorates: XLite\API\OpenApiTagsRepository

  xcart.api.xc.ProductVariants.product_ProductVariants_product.sub_extension:
    class: XC\ProductVariants\API\Endpoint\ProductVariant\SubExtension\SubExtension
    tags: [!php/const XLite\API\Extension\CollectionSubExtension\CollectionSubExtensionInterface::COLLECTION_SUB_EXTENSION_TAG, !php/const XLite\API\Extension\ItemSubExtension\ItemSubExtensionInterface::ITEM_SUB_EXTENSION_TAG]

  XC\ProductVariants\Controller\API\ProductVariant\Delete:
    tags: ['controller.service_arguments']

  XC\ProductVariants\Controller\API\ProductVariant\Put:
    tags: ['controller.service_arguments']

  XC\ProductVariants\Transformer\XC\ProductVariants\Model\ProductVariant\Logic\Entity\Storefront\ProductVariant:
    class: XC\ProductVariants\Transformer\XC\ProductVariants\Model\ProductVariant\Logic\Entity\Storefront\ProductVariant
    arguments:
      $factory: '@XCart\Factory\Logic\Entity\Storefront\ProductVariantInterface'

  XC\ProductVariants\Transformer\XLite\Model\Product\Logic\Entity\Storefront\ProductDecorator:
    decorates: XCart\Transformer\XLite\Model\Product\Logic\Entity\Storefront\Product
    arguments:
      $variantTransformer: '@XC\ProductVariants\Transformer\XC\ProductVariants\Model\ProductVariant\Logic\Entity\Storefront\ProductVariant'

  XC\ProductVariants\Transformer\XLite\Model\OrderItem\Logic\Entity\Storefront\Cart\ItemDecorator:
    class: XC\ProductVariants\Transformer\XLite\Model\OrderItem\Logic\Entity\Storefront\Cart\ItemDecorator
    decorates: XCart\Transformer\XLite\Model\OrderItem\Logic\Entity\Storefront\Cart\Item
    decoration_priority: 50
    arguments:
      $variantTransformer: '@XC\ProductVariants\Transformer\XC\ProductVariants\Model\ProductVariant\Logic\Entity\Storefront\ProductVariant'

  XC\ProductVariants\Transformer\XLite\Model\OrderItem\Logic\Entity\Storefront\Cart\NewItemDecorator:
    class: XC\ProductVariants\Transformer\XLite\Model\OrderItem\Logic\Entity\Storefront\Cart\NewItemDecorator
    decorates: XCart\Transformer\XLite\Model\OrderItem\Logic\Entity\Storefront\Cart\NewItem
    decoration_priority: 50
    arguments:
      $variantTransformer: '@XC\ProductVariants\Transformer\XC\ProductVariants\Model\ProductVariant\Logic\Entity\Storefront\ProductVariant'

  XC\ProductVariants\Logic\Action\Storefront\CartItem\CreateOne\Assembler\EntityAssemblerDecorator.create_one:
    class: XC\ProductVariants\Logic\Action\Storefront\CartItem\CreateOne\Assembler\EntityAssemblerDecorator
    decorates: xcart.logic.storefront.cart.item.create_one.entity.assembler
    decoration_priority: 50

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

  xcart.repository.product_variant.read:
    class: XC\ProductVariants\Model\Repo\ProductVariant
    factory: ['@doctrine.orm.default_entity_manager', getRepository]
    calls:
      - [setQueryBuilderFactory, ['@xcart.query_builder.factory.product_variant.read']]
    arguments:
      - XC\ProductVariants\Model\ProductVariant

  xcart.data_source.product_variant.read:
    class: XCart\Bundle\DoctrineBridgeBundle\DataSource\DoctrineReadDataSource
    arguments:
      $repository: '@xcart.repository.product_variant.read'
      $collectionClassName: 'XC\ProductVariants\Entity\ProductVariantCollection'

  XC\ProductVariants\EntityCallback\ProductVariantCallback:
    tags:
      - {name: doctrine.orm.entity_listener, event: prePersist, entity: XC\ProductVariants\Model\ProductVariant}
      - {name: doctrine.orm.entity_listener, event: postUpdate, entity: XC\ProductVariants\Model\ProductVariant}
