<?xml version="1.0"?>
<!--
/**
* @author Amasty Team
* @copyright Copyright (c) 2022 Amasty (https://www.amasty.com)
* @package Grouped Options 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_grouped_options_group" resource="default" engine="innodb" comment="Amasty Group Table">
        <column xsi:type="smallint" name="group_id" padding="6" unsigned="false" nullable="false" identity="true" comment="Group_id"/>
        <column xsi:type="smallint" name="attribute_id" padding="5" unsigned="true" nullable="false" identity="false" comment="Attribute_id"/>
        <column xsi:type="varchar" name="name" nullable="false" length="255" comment="Name"/>
        <column xsi:type="varchar" name="group_code" nullable="false" length="50" comment="Group_code"/>
        <column xsi:type="varchar" name="url" nullable="true" length="255" comment="Url"/>
        <column xsi:type="smallint" name="position" padding="6" unsigned="false" nullable="false" identity="false" default="0" comment="Position"/>
        <column xsi:type="varchar" name="visual" nullable="true" length="255" comment="Visual"/>
        <column xsi:type="smallint" name="type" padding="6" unsigned="false" nullable="false" identity="false" default="0" comment="Type"/>
        <column xsi:type="boolean" name="enabled" nullable="false" comment="Enabled"/>
        <constraint xsi:type="primary" referenceId="PRIMARY">
            <column name="group_id"/>
        </constraint>
        <constraint xsi:type="foreign" referenceId="AMASTY_GROUP_ATTR_ID_EAV_ATTR_ATTR_ID" table="amasty_grouped_options_group" column="attribute_id" referenceTable="eav_attribute" referenceColumn="attribute_id" onDelete="CASCADE" />
        <constraint xsi:type="unique" referenceId="AMASTY_GROUP_ATTR_ID_GROUP_CODE">
            <column name="attribute_id"/>
            <column name="group_code"/>
        </constraint>
        <index referenceId="AMASTY_GROUP_GROUP_CODE" indexType="btree">
            <column name="group_code"/>
        </index>
        <index referenceId="AMASTY_GROUP_POSITION" indexType="btree">
            <column name="position"/>
        </index>
        <index referenceId="AMASTY_GROUP_ENABLED" indexType="btree">
            <column name="enabled"/>
        </index>
        <index referenceId="AMASTY_GROUP_ATTRIBUTE_ID" indexType="btree">
            <column name="attribute_id"/>
        </index>
    </table>
    <table name="amasty_grouped_options_group_option" resource="default" engine="innodb" comment="amasty_grouped_options_group_option">
        <column xsi:type="int" name="group_option_id" padding="6" unsigned="false" nullable="false" identity="true" comment="Group_option_id"/>
        <column xsi:type="smallint" name="group_id" padding="6" unsigned="false" nullable="false" identity="false" comment="Group_id"/>
        <column xsi:type="int" name="option_id" padding="10" unsigned="true" nullable="false" identity="false" comment="Option ID"/>
        <column xsi:type="smallint" name="sort_order" padding="5" unsigned="true" nullable="false" identity="false" default="0" comment="Sort Order"/>
        <constraint xsi:type="primary" referenceId="PRIMARY">
            <column name="group_option_id"/>
        </constraint>
        <constraint xsi:type="foreign" referenceId="AMASTY_GROUP_OPT_GRP_ID_AMASTY_GROUP_GRP_ID" table="amasty_grouped_options_group_option" column="group_id" referenceTable="amasty_grouped_options_group" referenceColumn="group_id" onDelete="CASCADE"/>
        <constraint xsi:type="foreign" referenceId="AMASTY_GROUP_OPT_OPT_ID_EAV_ATTR_OPT_OPT_ID" table="amasty_grouped_options_group_option" column="option_id" referenceTable="eav_attribute_option" referenceColumn="option_id" onDelete="CASCADE"/>
        <constraint xsi:type="unique" referenceId="AMASTY_GROUP_OPT_GROUP_ID_OPTION_ID">
            <column name="group_id"/>
            <column name="option_id"/>
        </constraint>
        <index referenceId="AMASTY_GROUP_OPT_GROUP_ID" indexType="btree">
            <column name="group_id"/>
        </index>
    </table>
    <table name="amasty_grouped_options_group_value" resource="default" engine="innodb" comment="amasty_grouped_options_group_value">
        <column xsi:type="smallint" name="group_option_id" padding="6" unsigned="false" nullable="false" identity="true" comment="Group_option_id"/>
        <column xsi:type="smallint" name="group_id" padding="6" unsigned="false" nullable="false" identity="false" comment="Group_id"/>
        <column xsi:type="varchar" name="value" nullable="false" default="" comment="Option Value" onCreate="migrateDataFrom(value)" length="20"/>
        <column xsi:type="smallint" name="sort_order" padding="5" unsigned="true" nullable="false" identity="false" default="0" comment="Sort Order"/>
        <constraint xsi:type="primary" referenceId="PRIMARY">
            <column name="group_option_id"/>
        </constraint>
        <constraint xsi:type="foreign" referenceId="AMASTY_GROUP_VALUE_GRP_ID_AMASTY_GROUP_GRP_ID" table="amasty_grouped_options_group_value" column="group_id" referenceTable="amasty_grouped_options_group" referenceColumn="group_id" onDelete="CASCADE"/>
    </table>
</schema>
