<?xml version="1.0"?>
<!--
/**
 * @author Amasty Team
 * @copyright Copyright (c) Amasty (https://www.amasty.com)
 * @package Visual Merchandiser 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="catalog_category_product_static" resource="default" engine="innodb" comment="Catalog Product Category Linkage Extra Table">
        <column xsi:type="int" name="category_id" padding="10" unsigned="true" nullable="false" identity="false" default="0" comment="Page ID"/>
        <column xsi:type="int" name="product_id" padding="10" unsigned="true" nullable="false" identity="false" default="0" comment="Product ID"/>
        <column xsi:type="smallint" name="store_id" padding="5" unsigned="true" nullable="false" identity="false" default="0" comment="Store ID" disabled="true"/>
        <column xsi:type="int" name="position" padding="11" unsigned="false" nullable="false" identity="false" default="0" comment="Position"/>

        <constraint xsi:type="foreign"
                    referenceId="CAT_CTGR_PRD_STATIC_PRD_ID_CAT_PRD_ENTT_ENTT_ID"
                    table="catalog_category_product_static"
                    column="product_id"
                    referenceTable="catalog_product_entity"
                    referenceColumn="entity_id"
                    onDelete="CASCADE"/>

        <constraint xsi:type="foreign"
                    referenceId="CAT_CTGR_PRD_STATIC_CTGR_ID_CAT_CTGR_ENTT_ENTT_ID"
                    table="catalog_category_product_static" column="category_id"
                    referenceTable="catalog_category_entity"
                    referenceColumn="entity_id"
                    onDelete="CASCADE"/>

        <constraint xsi:type="unique" referenceId="CATALOG_CATEGORY_PRODUCT_STATIC_CATEGORY_ID_PRODUCT_ID_STORE_ID" disabled="true">
            <column name="category_id"/>
            <column name="product_id"/>
            <column name="store_id"/>
        </constraint>

        <constraint xsi:type="unique" referenceId="CATALOG_CATEGORY_PRODUCT_STATIC_CATEGORY_ID_PRODUCT_ID">
            <column name="category_id"/>
            <column name="product_id"/>
        </constraint>

        <index referenceId="CATALOG_CATEGORY_PRODUCT_STATIC_PRODUCT_ID" indexType="btree">
            <column name="product_id"/>
        </index>

        <index referenceId="CATALOG_CATEGORY_PRODUCT_STATIC_CATEGORY_ID" indexType="btree">
            <column name="category_id"/>
        </index>
    </table>

    <table name="amasty_merch_dynamic_category_products_temporary" resource="default" engine="innodb"
           comment="Amasty Merch Temporary Dynamic Category Products">
        <column xsi:type="varchar" name="hash" nullable="false" length="255" comment="Hash of Conditions"/>
        <column xsi:type="int" name="category_id" unsigned="true" nullable="false" identity="false"
                comment="Category ID"/>
        <column xsi:type="text" name="matched_ids" nullable="false" comment="Matched Product Ids"/>
        <column xsi:type="timestamp" name="created_at" on_update="false" nullable="false" default="CURRENT_TIMESTAMP"
                comment="Created At"/>
        <constraint xsi:type="primary" referenceId="PRIMARY">
            <column name="hash"/>
        </constraint>
    </table>
    <table name="amasty_merch_dynamic_category_products" resource="default" engine="innodb"
           comment="Amasty Merch Dynamic Category Products">
        <column xsi:type="int" name="category_id" unsigned="true" nullable="false" identity="false"
                comment="Category ID"/>
        <column xsi:type="smallint" name="store_id" unsigned="true" nullable="false" identity="false"
                comment="Store ID"/>
        <column xsi:type="int" name="product_id" unsigned="true" nullable="false" identity="false"
                comment="Product ID"/>

        <constraint xsi:type="primary" referenceId="PRIMARY">
            <column name="category_id"/>
            <column name="store_id"/>
            <column name="product_id"/>
        </constraint>

        <index referenceId="AMASTY_MERCH_DYNAMIC_CATEGORY_PRODUCTS_PRODUCT_ID" indexType="btree">
            <column name="product_id"/>
        </index>
    </table>
    <table name="amasty_merch_dynamic_category_products_replica" resource="default" engine="innodb"
           comment="Amasty Merch Dynamic Category Products">
        <column xsi:type="int" name="category_id" unsigned="true" nullable="false" identity="false"
                comment="Category ID"/>
        <column xsi:type="smallint" name="store_id" unsigned="true" nullable="false" identity="false"
                comment="Store ID"/>
        <column xsi:type="int" name="product_id" unsigned="true" nullable="false" identity="false"
                comment="Product ID"/>

        <constraint xsi:type="primary" referenceId="PRIMARY">
            <column name="category_id"/>
            <column name="store_id"/>
            <column name="product_id"/>
        </constraint>

        <index referenceId="AMASTY_MERCH_DYNAMIC_CATEGORY_PRODUCTS_PRODUCT_ID" indexType="btree">
            <column name="product_id"/>
        </index>
    </table>
</schema>
