<?xml version="1.0"?>
<!--
/**
 * @author Amasty Team
 * @copyright Copyright (c) Amasty (https://www.amasty.com)
 * @package Product Import Entity for Magento 2 (System)
 */-->
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:noNamespaceSchemaLocation="urn:amasty:module:Amasty_ImportCore:etc/am_import.xsd">
    <entity code="downloadable_link">
        <name>Downloadable Product Link</name>
        <group>Product</group>
        <description>Downloadable Link Import Entity</description>
        <enabledChecker class="Amasty\ImportCore\Import\Config\IsModuleEnabled">
            <arguments>
                <argument name="moduleName" xsi:type="string">Magento_Downloadable</argument>
            </arguments>
        </enabledChecker>
        <fieldsConfig>
            <fieldsClass class="Amasty\ImportCore\Import\FieldsClass\Describe">
                <arguments>
                    <argument name="tableName" xsi:type="string">downloadable_link</argument>
                </arguments>
            </fieldsClass>
            <fields>
                <field name="link_id">
                    <actions>
                        <trim/>
                    </actions>
                    <validation>
                        <integer/>
                        <greaterThanZero/>
                    </validation>
                </field>
                <field name="product_id">
                    <actions>
                        <trim/>
                    </actions>
                    <validation>
                        <integer/>
                        <greaterThanZero/>
                        <notEmpty/>
                        <custom class="Amasty\ProductImportEntity\Import\Validation\EntityValidator\Product\LinkField"
                                error="Product entity with specified 'product_id' doesn't exist"
                                rootOnly="true">
                            <excludeBehaviors>
                                <deleteDirect/>
                            </excludeBehaviors>
                        </custom>
                        <custom class="Amasty\ProductImportEntity\Import\Validation\EntityValidator\Product\Type\Downloadable"
                                error="The product needs to be the downloadable type. Verify the product and try again."/>
                    </validation>
                    <required/>
                </field>
                <field name="sort_order">
                    <actions>
                        <trim/>
                    </actions>
                    <validation>
                        <integer/>
                        <nonNegative/>
                    </validation>
                </field>
                <field name="number_of_downloads">
                    <actions>
                        <trim/>
                    </actions>
                    <validation>
                        <integer/>
                        <nonNegative/>
                    </validation>
                </field>
                <field name="is_shareable">
                    <actions>
                        <trim/>
                    </actions>
                    <validation>
                        <integer/>
                        <notEmpty isZeroValueAllowed="true"/>
                        <custom class="Amasty\ProductImportEntity\Import\Validation\EntityValidator\Downloadable\Link\Shareable"
                                error="Please specify the valid value of the 'is_shareable' field"/>
                    </validation>
                    <required>
                        <excludeBehaviors>
                            <updateDirect/>
                        </excludeBehaviors>
                    </required>
                </field>
                <field name="link_type">
                    <filter>
                        <type>select</type>
                        <options>
                            <class>Magento\Downloadable\Model\Source\TypeUpload</class>
                        </options>
                    </filter>
                </field>
            </fields>
            <sampleData>
                <row>
                    <field name='link_id'>1</field>
                    <field name='product_id'>1</field>
                    <field name='sort_order'>1</field>
                    <field name='number_of_downloads'>100</field>
                    <field name='is_shareable'>1</field>
                    <field name='link_file'>/d/o/document.pdf</field>
                    <field name='link_type'>file</field>
                    <field name='sample_type'>url</field>
                    <field name='sample_url'>https://example.com/sample.pdf</field>
                </row>
                <row>
                    <field name='link_id'>2</field>
                    <field name='product_id'>1</field>
                    <field name='sort_order'>2</field>
                    <field name='number_of_downloads'>200</field>
                    <field name='is_shareable'>1</field>
                    <field name='link_url'>https://example.com/melody.ogg</field>
                    <field name='link_type'>url</field>
                    <field name='sample_type'>file</field>
                    <field name='sample_file'>/m/e/melody-sample.ogg</field>
                </row>
            </sampleData>
        </fieldsConfig>
        <behaviors>
            <addUpdateDirect name="Add/Update">
                <tableName>downloadable_link</tableName>
                <idField>link_id</idField>
            </addUpdateDirect>
            <addDirect name="Only add">
                <tableName>downloadable_link</tableName>
                <idField>link_id</idField>
            </addDirect>
            <updateDirect name="Only update">
                <tableName>downloadable_link</tableName>
                <idField>link_id</idField>
            </updateDirect>
            <deleteDirect name="Delete">
                <tableName>downloadable_link</tableName>
                <idField>link_id</idField>
            </deleteDirect>
        </behaviors>
    </entity>
    <relation code="product_downloadable_link">
        <parent_entity>catalog_product_entity</parent_entity>
        <child_entity>downloadable_link</child_entity>
        <sub_entity_field_name>downloadable_link</sub_entity_field_name>
        <skip_relation_fields_update>true</skip_relation_fields_update>
        <type>one_to_many</type>
        <arguments>
            <argument name="parent_entity_name" xsi:type="string">Magento\Catalog\Api\Data\ProductInterface</argument>
            <argument name="child_field_name" xsi:type="string">product_id</argument>
        </arguments>
    </relation>
</config>
