<?xml version="1.0"?>
<!--
/**
 * @author Amasty Team
 * @copyright Copyright (c) Amasty (https://www.amasty.com)
 * @package Custom Checkout Fields for Magento 2
 */-->

<schema xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:noNamespaceSchemaLocation="urn:magento:framework:Setup/Declaration/Schema/etc/schema.xsd">
    <table name="amasty_order_attribute_entity"
           resource="default"
           engine="innodb"
           comment="Amasty Order Attribute entity table">
        <column xsi:type="int"
                name="entity_id"
                nullable="false"
                default="0"
                unsigned="true"
                comment="Order Attribute Entity ID" />
        <column xsi:type="int"
                name="parent_id"
                nullable="false"
                default="0"
                comment="Order Attribute Entity Parent ID" />
        <column xsi:type="smallint"
                name="parent_entity_type"
                nullable="false"
                default="0"
                comment="Order Attribute Entity type" />

        <constraint xsi:type="primary" referenceId="PRIMARY">
            <column name="entity_id" />
            <column name="parent_entity_type" />
        </constraint>

        <constraint xsi:type="unique" referenceId="AMASTY_ORDER_ATTRIBUTE_ENTITY_PARENT_ID_PARENT_ENTITY_TYPE">
            <column name="parent_id" />
            <column name="parent_entity_type" />
        </constraint>

        <constraint xsi:type="foreign"
                    referenceId="AMASTY_ORDER_ATTRIBUTE_ENTITY_ENTITY_ID_AMASTY_ORDER_ATTRIBUTE_ENTITY_INCREMENT_ENTITY_ID"
                    table="amasty_order_attribute_entity"
                    column="entity_id"
                    referenceTable="amasty_order_attribute_entity_increment"
                    referenceColumn="entity_id"
                    onDelete="CASCADE" />
    </table>

    <table name="amasty_order_attribute_entity_increment"
           resource="default"
           engine="innodb"
           comment="Amasty Order Attribute entity increment table">
        <column xsi:type="int"
                name="entity_id"
                nullable="false"
                unsigned="true"
                identity="true"
                onCreate="AmorderattrMigrateEntityId(amasty_order_attribute_entity,entity_id)" />

        <constraint xsi:type="primary" referenceId="PRIMARY">
            <column name="entity_id"/>
        </constraint>
    </table>

    <table name="amasty_order_attribute_eav_attribute"
           resource="default"
           engine="innodb"
           comment="Amasty Order Attribute EAV extension table">
        <column xsi:type="int"
                name="id"
                padding="10"
                unsigned="true"
                nullable="false"
                identity="true"
                comment="ID" />
        <column xsi:type="smallint"
                name="attribute_id"
                nullable="false"
                default="0"
                unsigned="true"
                comment="Order Attribute EAV Attribute ID" />
        <column xsi:type="smallint"
                name="is_visible_on_front"
                nullable="false"
                default="0"
                comment="Frontend visibility" />
        <column xsi:type="boolean"
                name="is_hidden_from_customer"
                nullable="false"
                default="false"
                comment="Is hidden from customer" />
        <column xsi:type="boolean"
                name="is_visible_on_back"
                nullable="true"
                default="false"
                comment="Backend visibility" />
        <column xsi:type="smallint"
                name="multiselect_size"
                nullable="true"
                default="0"
                comment="Multiselect size" />
        <column xsi:type="int"
                name="sorting_order"
                nullable="true"
                default="0"
                comment="Sorting order" />
        <column xsi:type="smallint" name="checkout_step" nullable="true" default="0" comment="Checkout Step" />
        <column xsi:type="boolean"
                name="show_on_grids"
                nullable="true"
                default="false"
                comment="Show on Admin grids" />
        <column xsi:type="boolean"
                name="include_in_pdf"
                nullable="true"
                default="false"
                comment="Include to PDF documents" />
        <column xsi:type="boolean"
                name="include_in_html_print_order"
                nullable="true"
                default="false"
                comment="Include to HTML print order" />
        <column xsi:type="boolean"
                name="save_to_future_checkout"
                nullable="true"
                default="false"
                comment="Save Attribute to future checkout" />
        <column xsi:type="boolean"
                name="apply_default_value"
                nullable="true"
                default="false"
                comment="Apply Default value to Attribute" />
        <column xsi:type="boolean"
                name="include_in_email"
                nullable="true"
                default="true"
                comment="Include Attribute in Emails" />
        <column xsi:type="boolean"
                name="required_on_front_only"
                nullable="true"
                default="false"
                comment="Is attribute required" />
        <column xsi:type="text" name="validate_rules" comment="Validate Rules" />
        <column xsi:type="varchar" name="input_filter" length="255" comment="Input Filter" />
        <column name="conditions_serialized" xsi:type="text" nullable="true" comment="Conditions"/>
        <index referenceId="AMASTY_ORDER_ATTRIBUTE_EAV_ATTRIBUTE_ATTRIBUTE_ID" indexType="btree">
            <column name="attribute_id" />
        </index>
        <constraint xsi:type="primary" referenceId="PRIMARY">
            <column name="id"/>
        </constraint>
        <constraint xsi:type="foreign"
                    referenceId="AMASTY_ORDER_ATTR_EAV_ATTR_ATTR_ID_EAV_ATTR_ATTR_ID"
                    table="amasty_order_attribute_eav_attribute"
                    column="attribute_id"
                    referenceTable="eav_attribute"
                    referenceColumn="attribute_id"
                    onDelete="CASCADE" />
    </table>

    <table name="amasty_order_attribute_relation"
           resource="default"
           engine="innodb"
           comment="Amasty Order Attribute Relation Table">
        <column xsi:type="int"
                name="relation_id"
                identity="true"
                unsigned="true"
                nullable="false"
                comment="Order Attribute Relation ID" />
        <column xsi:type="varchar"
                name="name"
                length="255"
                nullable="true"
                comment="Order Attribute Relation Name" />

        <constraint xsi:type="primary" referenceId="PRIMARY">
            <column name="relation_id" />
        </constraint>
    </table>

    <table name="amasty_order_attribute_relation_details"
           resource="default"
           engine="innodb"
           comment="Amasty Order Attribute Relation Details Table">
        <column xsi:type="int"
                name="relation_detail_id"
                identity="true"
                unsigned="true"
                nullable="false"
                comment="Order Attribute Relation Details ID" />
        <column xsi:type="smallint"
                name="attribute_id"
                nullable="false"
                default="0"
                unsigned="true"
                comment="Order Attribute Relation Attribute ID" />
        <column xsi:type="int"
                name="option_id"
                nullable="false"
                default="0"
                unsigned="true"
                comment="Order Attribute Option ID" />
        <column xsi:type="smallint"
                name="dependent_attribute_id"
                nullable="false"
                default="0"
                comment="Order Attribute Dependent Attribute ID" />
        <column xsi:type="int"
                name="relation_id"
                nullable="false"
                default="0"
                comment="Order Attribute Relation Details for Relation ID" />

        <constraint xsi:type="primary" referenceId="PRIMARY">
            <column name="relation_detail_id" />
        </constraint>
    </table>

    <table name="amasty_order_attribute_eav_attribute_customer_group"
           resource="default"
           engine="innodb"
           comment="Amasty Order Attribute Customer Group Relation table">
        <column xsi:type="int"
                name="id"
                padding="10"
                unsigned="true"
                nullable="false"
                identity="true"
                comment="ID" />
        <column xsi:type="smallint"
                name="attribute_id"
                nullable="false"
                default="0"
                unsigned="true"
                comment="Order Attribute Entity ID" />
        <column xsi:type="int"
                name="customer_group_id"
                unsigned="true"
                nullable="false"
                padding="10"
                comment="Customer Group ID" />

        <constraint xsi:type="unique" referenceId="AMASTY_ORDER_ATTR_EAV_ATTR_CSTR_GROUP_ATTR_ID_CSTR_GROUP_ID">
            <column name="attribute_id" />
            <column name="customer_group_id" />
        </constraint>
        <constraint xsi:type="primary" referenceId="PRIMARY">
            <column name="id"/>
        </constraint>
        <constraint xsi:type="foreign"
                    referenceId="AMASTY_ORDER_ATTR_EAV_ATTR_CSTR_GROUP_ATTR_ID_EAV_ATTR_ATTR_ID"
                    table="amasty_order_attribute_eav_attribute_customer_group"
                    column="attribute_id"
                    referenceTable="eav_attribute"
                    referenceColumn="attribute_id"
                    onDelete="CASCADE" />

        <constraint xsi:type="foreign"
                    referenceId="FK_04223CB6C4DD98333CEE67098AE81BE4"
                    table="amasty_order_attribute_eav_attribute_customer_group"
                    column="customer_group_id"
                    referenceTable="customer_group"
                    referenceColumn="customer_group_id"
                    onDelete="CASCADE" />
    </table>

    <table name="amasty_order_attribute_eav_attribute_store"
           resource="default"
           engine="innodb"
           comment="Amasty Order Attribute Store Relation table">
        <column xsi:type="int"
                name="id"
                padding="10"
                unsigned="true"
                nullable="false"
                identity="true"
                comment="ID" />
        <column xsi:type="smallint"
                name="attribute_id"
                nullable="false"
                default="0"
                unsigned="true"
                comment="Order Attribute Entity ID" />
        <column xsi:type="smallint" name="store_id" unsigned="true" nullable="false" comment="Store Id" />
        <constraint xsi:type="primary" referenceId="PRIMARY">
            <column name="id"/>
        </constraint>
        <constraint xsi:type="unique"
                    referenceId="AMASTY_ORDER_ATTRIBUTE_EAV_ATTRIBUTE_STORE_ATTRIBUTE_ID_STORE_ID">
            <column name="attribute_id" />
            <column name="store_id" />
        </constraint>

        <constraint xsi:type="foreign"
                    referenceId="AMASTY_ORDER_ATTR_EAV_ATTR_STORE_ATTR_ID_EAV_ATTR_ATTR_ID"
                    table="amasty_order_attribute_eav_attribute_store"
                    column="attribute_id"
                    referenceTable="eav_attribute"
                    referenceColumn="attribute_id"
                    onDelete="CASCADE" />

        <constraint xsi:type="foreign"
                    referenceId="AMASTY_ORDER_ATTR_EAV_ATTR_STORE_STORE_ID_STORE_STORE_ID"
                    table="amasty_order_attribute_eav_attribute_store"
                    column="store_id"
                    referenceTable="store"
                    referenceColumn="store_id"
                    onDelete="CASCADE" />
    </table>

    <table name="amasty_order_attribute_entity_int"
           resource="default"
           engine="innodb"
           comment="Order Attribute Value Int">
        <column xsi:type="int" name="value_id" identity="true" unsigned="true" nullable="false" comment="Value ID" />
        <column xsi:type="smallint"
                name="attribute_id"
                unsigned="true"
                nullable="false"
                default="0"
                comment="Attribute ID" />
        <column xsi:type="int" name="entity_id" unsigned="true" nullable="false" default="0" comment="Entity ID" />
        <column xsi:type="int" name="value" nullable="true" default="null" comment="Value" />

        <constraint xsi:type="primary" referenceId="PRIMARY">
            <column name="value_id" />
        </constraint>

        <constraint xsi:type="unique" referenceId="AMASTY_ORDER_ATTRIBUTE_ENTITY_INT_ENTITY_ID_ATTRIBUTE_ID">
            <column name="entity_id" />
            <column name="attribute_id" />
        </constraint>

        <constraint xsi:type="foreign"
                    referenceId="AMASTY_ORDER_ATTR_ENTT_INT_ATTR_ID_EAV_ATTR_ATTR_ID"
                    table="amasty_order_attribute_entity_int"
                    column="attribute_id"
                    referenceTable="eav_attribute"
                    referenceColumn="attribute_id"
                    onDelete="CASCADE" />

        <constraint xsi:type="foreign"
                    referenceId="FK_931E3A58BEC4300F3C596255DB6AE576"
                    table="amasty_order_attribute_entity_int"
                    column="entity_id"
                    referenceTable="amasty_order_attribute_entity"
                    referenceColumn="entity_id"
                    onDelete="CASCADE" />
    </table>

    <table name="amasty_order_attribute_entity_decimal"
           resource="default"
           engine="innodb"
           comment="Order Attribute Value Decimal">
        <column xsi:type="int" name="value_id" identity="true" unsigned="true" nullable="false" comment="Value ID" />
        <column xsi:type="smallint"
                name="attribute_id"
                unsigned="true"
                nullable="false"
                default="0"
                comment="Attribute ID" />
        <column xsi:type="int" name="entity_id" unsigned="true" nullable="false" default="0" comment="Entity ID" />
        <column xsi:type="decimal"
                name="value"
                scale="4"
                precision="12"
                nullable="true"
                comment="Value" />

        <constraint xsi:type="primary" referenceId="PRIMARY">
            <column name="value_id" />
        </constraint>

        <constraint xsi:type="unique" referenceId="AMASTY_ORDER_ATTRIBUTE_ENTITY_DECIMAL_ENTITY_ID_ATTRIBUTE_ID">
            <column name="entity_id" />
            <column name="attribute_id" />
        </constraint>

        <constraint xsi:type="foreign"
                    referenceId="AMASTY_ORDER_ATTR_ENTT_DEC_ATTR_ID_EAV_ATTR_ATTR_ID"
                    table="amasty_order_attribute_entity_decimal"
                    column="attribute_id"
                    referenceTable="eav_attribute"
                    referenceColumn="attribute_id"
                    onDelete="CASCADE" />

        <constraint xsi:type="foreign"
                    referenceId="FK_EA6C9BA46FE7C0B8F605ED028512F65A"
                    table="amasty_order_attribute_entity_decimal"
                    column="entity_id"
                    referenceTable="amasty_order_attribute_entity"
                    referenceColumn="entity_id"
                    onDelete="CASCADE" />
    </table>

    <table name="amasty_order_attribute_entity_datetime"
           resource="default"
           engine="innodb"
           comment="Order Attribute Value DateTime">
        <column xsi:type="int" name="value_id" identity="true" unsigned="true" nullable="false" comment="Value ID" />
        <column xsi:type="smallint"
                name="attribute_id"
                unsigned="true"
                nullable="false"
                default="0"
                comment="Attribute ID" />
        <column xsi:type="int" name="entity_id" unsigned="true" nullable="false" default="0" comment="Entity ID" />
        <column xsi:type="datetime" name="value" nullable="true" default="NULL" comment="Value" />

        <constraint xsi:type="primary" referenceId="PRIMARY">
            <column name="value_id" />
        </constraint>

        <constraint xsi:type="unique" referenceId="AMASTY_ORDER_ATTRIBUTE_ENTITY_DATETIME_ENTITY_ID_ATTRIBUTE_ID">
            <column name="entity_id" />
            <column name="attribute_id" />
        </constraint>

        <constraint xsi:type="foreign"
                    referenceId="AMASTY_ORDER_ATTR_ENTT_DTIME_ATTR_ID_EAV_ATTR_ATTR_ID"
                    table="amasty_order_attribute_entity_datetime"
                    column="attribute_id"
                    referenceTable="eav_attribute"
                    referenceColumn="attribute_id"
                    onDelete="CASCADE" />

        <constraint xsi:type="foreign"
                    referenceId="FK_327660221969FD7E22674F7A7999A8D8"
                    table="amasty_order_attribute_entity_datetime"
                    column="entity_id"
                    referenceTable="amasty_order_attribute_entity"
                    referenceColumn="entity_id"
                    onDelete="CASCADE" />
    </table>

    <table name="amasty_order_attribute_entity_text"
           resource="default"
           engine="innodb"
           comment="Order Attribute Value Text">
        <column xsi:type="int" name="value_id" identity="true" unsigned="true" nullable="false" comment="Value ID" />
        <column xsi:type="smallint"
                name="attribute_id"
                unsigned="true"
                nullable="false"
                default="0"
                comment="Attribute ID" />
        <column xsi:type="int" name="entity_id" unsigned="true" nullable="false" default="0" comment="Entity ID" />
        <column xsi:type="text" name="value" nullable="false" comment="Value" />

        <constraint xsi:type="primary" referenceId="PRIMARY">
            <column name="value_id" />
        </constraint>

        <constraint xsi:type="unique" referenceId="AMASTY_ORDER_ATTRIBUTE_ENTITY_TEXT_ENTITY_ID_ATTRIBUTE_ID">
            <column name="entity_id" />
            <column name="attribute_id" />
        </constraint>

        <constraint xsi:type="foreign"
                    referenceId="AMASTY_ORDER_ATTR_ENTT_TEXT_ATTR_ID_EAV_ATTR_ATTR_ID"
                    table="amasty_order_attribute_entity_text"
                    column="attribute_id"
                    referenceTable="eav_attribute"
                    referenceColumn="attribute_id"
                    onDelete="CASCADE" />

        <constraint xsi:type="foreign"
                    referenceId="FK_AB65AF7AD3ECE7F713B783FC04F4E2DB"
                    table="amasty_order_attribute_entity_text"
                    column="entity_id"
                    referenceTable="amasty_order_attribute_entity"
                    referenceColumn="entity_id"
                    onDelete="CASCADE" />
    </table>

    <table name="amasty_order_attribute_entity_varchar"
           resource="default"
           engine="innodb"
           comment="Order Attribute Value VarChar">
        <column xsi:type="int" name="value_id" identity="true" unsigned="true" nullable="false" comment="Value ID" />
        <column xsi:type="smallint"
                name="attribute_id"
                unsigned="true"
                nullable="false"
                default="0"
                comment="Attribute ID" />
        <column xsi:type="int" name="entity_id" unsigned="true" nullable="false" default="0" comment="Entity ID" />
        <column xsi:type="varchar" name="value" length="255" nullable="true" comment="Value" />

        <constraint xsi:type="primary" referenceId="PRIMARY">
            <column name="value_id" />
        </constraint>

        <constraint xsi:type="unique" referenceId="AMASTY_ORDER_ATTRIBUTE_ENTITY_VARCHAR_ENTITY_ID_ATTRIBUTE_ID">
            <column name="entity_id" />
            <column name="attribute_id" />
        </constraint>

        <constraint xsi:type="foreign"
                    referenceId="AMASTY_ORDER_ATTR_ENTT_VCHR_ATTR_ID_EAV_ATTR_ATTR_ID"
                    table="amasty_order_attribute_entity_varchar"
                    column="attribute_id"
                    referenceTable="eav_attribute"
                    referenceColumn="attribute_id"
                    onDelete="CASCADE" />

        <constraint xsi:type="foreign"
                    referenceId="FK_8319DA88BD748E0ECA25B1F36F2D600C"
                    table="amasty_order_attribute_entity_varchar"
                    column="entity_id"
                    referenceTable="amasty_order_attribute_entity"
                    referenceColumn="entity_id"
                    onDelete="CASCADE" />
    </table>

    <table name="amasty_order_attribute_shipping_methods"
           resource="default"
           engine="innodb"
           comment="Amasty Order Attribute Shipping Methods">
        <column xsi:type="int"
                name="id"
                padding="10"
                unsigned="true"
                nullable="false"
                identity="true"
                comment="ID" />
        <column xsi:type="smallint"
                name="attribute_id"
                nullable="false"
                default="0"
                unsigned="true"
                comment="Order Attribute EAV Attribute ID" />
        <column xsi:type="varchar"
                name="shipping_method"
                length="255"
                nullable="true"
                comment="Order Attribute Shipping Method Code" />

        <constraint xsi:type="unique" referenceId="AMASTY_ORDER_ATTR_SHPP_METHODS_ATTR_ID_SHPP_METHOD">
            <column name="attribute_id" />
            <column name="shipping_method" />
        </constraint>
        <constraint xsi:type="primary" referenceId="PRIMARY">
            <column name="id"/>
        </constraint>
        <constraint xsi:type="foreign"
                    referenceId="AMASTY_ORDER_ATTR_SHPP_METHODS_ATTR_ID_EAV_ATTR_ATTR_ID"
                    table="amasty_order_attribute_shipping_methods"
                    column="attribute_id"
                    referenceTable="eav_attribute"
                    referenceColumn="attribute_id"
                    onDelete="CASCADE" />
    </table>

    <table name="amasty_order_attribute_tooltip"
           resource="default"
           engine="innodb"
           comment="Amasty Order Attribute Tooltip">
        <column xsi:type="int" name="id" identity="true" unsigned="true" nullable="false" comment="Id" />
        <column xsi:type="smallint" name="attribute_id" nullable="false" unsigned="true" comment="Attribute Id" />
        <column xsi:type="smallint" name="store_id" nullable="false" unsigned="true" comment="Store Id" />
        <column xsi:type="varchar" name="tooltip" length="512" nullable="false" comment="Tooltip" />

        <constraint xsi:type="primary" referenceId="PRIMARY">
            <column name="id" />
        </constraint>

        <constraint xsi:type="unique" referenceId="AMASTY_ORDER_ATTRIBUTE_TOOLTIP_ATTRIBUTE_ID_STORE_ID">
            <column name="attribute_id" />
            <column name="store_id" />
        </constraint>

        <constraint xsi:type="foreign"
                    referenceId="AMASTY_ORDER_ATTR_TOOLTIP_ATTR_ID_EAV_ATTR_ATTR_ID"
                    table="amasty_order_attribute_tooltip"
                    column="attribute_id"
                    referenceTable="eav_attribute"
                    referenceColumn="attribute_id"
                    onDelete="CASCADE" />

        <constraint xsi:type="foreign"
                    referenceId="AMASTY_ORDER_ATTRIBUTE_TOOLTIP_STORE_ID_STORE_STORE_ID"
                    table="amasty_order_attribute_tooltip"
                    column="store_id"
                    referenceTable="store"
                    referenceColumn="store_id"
                    onDelete="CASCADE" />
    </table>
    <table name="amasty_order_attribute_product_index"
           resource="default"
           engine="innodb"
           comment="Amasty Order Attributes Valid Products Index Table">
        <column xsi:type="smallint"
                name="attribute_id"
                unsigned="true"
                nullable="false"
                default="0"
                comment="Order Attribute Entity ID" />
        <column xsi:type="int"
                name="product_id"
                padding="10"
                unsigned="true"
                nullable="false"
                identity="false"
                default="0"
                comment="Product ID"/>
        <constraint xsi:type="primary" referenceId="PRIMARY">
            <column name="attribute_id"/>
            <column name="product_id"/>
        </constraint>
        <constraint xsi:type="foreign"
                    referenceId="AMASTY_ORDER_ATTR_PRD_IDX_ATTR_ID_EAV_ATTR_ATTR_ID"
                    table="amasty_order_attribute_product_index"
                    column="attribute_id"
                    referenceTable="eav_attribute"
                    referenceColumn="attribute_id"
                    disabled="true"
                    onDelete="CASCADE"/>
        <constraint xsi:type="foreign"
                    referenceId="AMASTY_ORDER_ATTR_PRD_IDX_PRD_ID_CAT_PRD_ENTT_ENTT_ID"
                    table="amasty_order_attribute_product_index"
                    column="product_id"
                    referenceTable="catalog_product_entity"
                    referenceColumn="entity_id"
                    disabled="true"
                    onDelete="CASCADE"/>
    </table>
    <table name="amasty_order_attribute_product_index_replica"
           resource="default"
           engine="innodb"
           comment="Amasty Order Attributes Valid Products Replica Table">
        <column xsi:type="smallint"
                name="attribute_id"
                unsigned="true"
                nullable="false"
                default="0"
                comment="Order Attribute Entity ID" />
        <column xsi:type="int"
                name="product_id"
                padding="10"
                unsigned="true"
                nullable="false"
                identity="false"
                default="0"
                comment="Product ID"/>
        <constraint xsi:type="primary" referenceId="PRIMARY">
            <column name="attribute_id"/>
            <column name="product_id"/>
        </constraint>
        <constraint xsi:type="foreign"
                    referenceId="AMASTY_ORDER_ATTR_PRD_IDX_REPLICA_ATTR_ID_EAV_ATTR_ATTR_ID"
                    table="amasty_order_attribute_product_index_replica"
                    column="attribute_id"
                    referenceTable="eav_attribute"
                    referenceColumn="attribute_id"
                    disabled="true"
                    onDelete="CASCADE"/>
        <constraint xsi:type="foreign"
                    referenceId="AMASTY_ORDER_ATTR_PRD_IDX_REPLICA_PRD_ID_CAT_PRD_ENTT_ENTT_ID"
                    table="amasty_order_attribute_product_index_replica"
                    column="product_id"
                    referenceTable="catalog_product_entity"
                    referenceColumn="entity_id"
                    disabled="true"
                    onDelete="CASCADE"/>
    </table>
</schema>
