<?xml version="1.0"?>
<!--
/**
 * @author Amasty Team
 * @copyright Copyright (c) Amasty (https://www.amasty.com)
 * @package Affiliate 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_affiliate_account" resource="default" engine="innodb" comment="amasty_affiliate_account">
        <column xsi:type="int" name="account_id" padding="10" unsigned="true" nullable="false" identity="true"
                comment="Account Id"/>
        <column xsi:type="int" name="customer_id" padding="10" unsigned="true" nullable="false" identity="false"
                comment="Customer Id"/>
        <column xsi:type="smallint" name="status" padding="5" unsigned="true" nullable="false" identity="false"
                default="1" comment="Status" onCreate="migrateDataFrom(is_affiliate_active)"/>
        <column xsi:type="boolean" name="is_affiliate_active" nullable="false" default="true"
                comment="DEPRECATED, use status"/>
        <column xsi:type="boolean" name="accepted_terms_conditions" nullable="false"
                comment="Is Terms Conditions Accepted"/>
        <column xsi:type="boolean" name="receive_notifications" nullable="false" comment="Receive Notifications"/>
        <column xsi:type="varchar" name="paypal_email" nullable="true" length="255" comment="Paypal Email"/>
        <column xsi:type="varchar" name="referring_code" nullable="true" length="255" comment="Referring Code"/>
        <column xsi:type="varchar" name="referring_website" nullable="true" length="255" comment="Referring Website"/>
        <column xsi:type="decimal" name="balance" scale="2" precision="12" unsigned="false" nullable="false" default="0"
                comment="Balance" onCreate="migrateDataFrom(balance)"/>
        <column xsi:type="decimal" name="on_hold" scale="2" precision="12" unsigned="true" nullable="false" default="0"
                comment="On Hold" onCreate="migrateDataFrom(on_hold)"/>
        <column xsi:type="decimal" name="commission_paid" scale="2" precision="12" unsigned="true" nullable="false"
                default="0" comment="Paid Commission" onCreate="migrateDataFrom(commission_paid)"/>
        <column xsi:type="decimal" name="lifetime_commission" scale="2" precision="12" unsigned="true" nullable="false"
                default="0" comment="Lifetime Commission" onCreate="migrateDataFrom(lifetime_commission)"/>
        <column xsi:type="int" name="widget_width" padding="10" unsigned="true" nullable="false" identity="false"
                default="350" comment="Widget Width"/>
        <column xsi:type="int" name="widget_height" padding="10" unsigned="true" nullable="false" identity="false"
                default="350" comment="Widget Height"/>
        <column xsi:type="varchar" name="widget_title" nullable="true" length="255" default="Bestsellers"
                comment="Widget Title"/>
        <column xsi:type="int" name="widget_products_num" padding="10" unsigned="true" nullable="true" identity="false"
                default="6" comment="Widget Products Num"/>
        <column xsi:type="varchar" name="widget_type" nullable="false" length="255" default="new"
                comment="Widget Type"/>
        <column xsi:type="boolean" name="widget_show_name" nullable="false" default="true" comment="Is Show Widget Name"/>
        <column xsi:type="boolean" name="widget_show_price" nullable="false" default="true"
                comment="Is Show Widget Price"/>
        <column xsi:type="boolean" name="is_custom_referring_code" nullable="false" comment="Is Custom Referring Code"/>
        <constraint xsi:type="primary" referenceId="PRIMARY">
            <column name="account_id"/>
        </constraint>
        <constraint xsi:type="foreign" referenceId="AMASTY_AFFILIATE_ACCOUNT_CUSTOMER_ID_CUSTOMER_ENTITY_ENTITY_ID"
                    table="amasty_affiliate_account" column="customer_id" referenceTable="customer_entity"
                    referenceColumn="entity_id" onDelete="CASCADE"/>
        <index referenceId="AMASTY_AFFILIATE_ACCOUNT_ACCOUNT_ID" indexType="btree">
            <column name="account_id"/>
        </index>
    </table>
    <table name="amasty_affiliate_transaction" resource="default" engine="innodb"
           comment="amasty_affiliate_transaction">
        <column xsi:type="int" name="transaction_id" padding="10" unsigned="true" nullable="false" identity="true"
                comment="Transaction Id"/>
        <column xsi:type="int" name="affiliate_account_id" padding="10" unsigned="true" nullable="false"
                identity="false" comment="Affiliate Account Id"/>
        <column xsi:type="int" name="program_id" padding="10" unsigned="true" nullable="false" identity="false"
                comment="Program Id"/>
        <column xsi:type="varchar" name="order_increment_id" nullable="true" length="32" comment="Order Increment Id"/>
        <column xsi:type="decimal" name="profit" scale="2" precision="12" unsigned="true" nullable="false" default="0"
                comment="Profit" onCreate="migrateDataFrom(profit)"/>
        <column xsi:type="decimal" name="balance" scale="2" precision="12" unsigned="false" nullable="false" default="0"
                comment="Balance" onCreate="migrateDataFrom(balance)"/>
        <column xsi:type="decimal" name="commission" scale="2" precision="12" unsigned="false" nullable="false"
                default="0" comment="Commission" onCreate="migrateDataFrom(commission)"/>
        <column xsi:type="decimal" name="discount" scale="2" precision="12" unsigned="false" nullable="false"
                default="0" comment="Discount" onCreate="migrateDataFrom(discount)"/>
        <column xsi:type="timestamp" name="updated_at" on_update="false" nullable="false" default="CURRENT_TIMESTAMP"
                comment="Updated At"/>
        <column xsi:type="varchar" name="type" nullable="false" length="255" comment="Type"/>
        <column xsi:type="varchar" name="status" nullable="false" length="255" comment="Status"/>
        <column xsi:type="smallint" name="balance_change_type" padding="6" unsigned="false" nullable="true"
                identity="false" default="0" comment="Balance Change Type"/>
        <constraint xsi:type="primary" referenceId="PRIMARY">
            <column name="transaction_id"/>
        </constraint>
        <constraint xsi:type="foreign" referenceId="FK_DB443F26218B8E96BBA081A8A362BAA1"
                    table="amasty_affiliate_transaction" column="affiliate_account_id"
                    referenceTable="amasty_affiliate_account" referenceColumn="account_id" onDelete="CASCADE"/>
        <index referenceId="AMASTY_AFFILIATE_TRANSACTION_TRANSACTION_ID" indexType="btree">
            <column name="transaction_id"/>
        </index>
    </table>
    <table name="amasty_affiliate_banner" resource="default" engine="innodb" comment="amasty_affiliate_banner">
        <column xsi:type="int" name="banner_id" padding="10" unsigned="true" nullable="false" identity="true"
                comment="Banner Id"/>
        <column xsi:type="text" name="title" nullable="true" comment="Title"/>
        <column xsi:type="varchar" name="type" nullable="false" length="255" comment="Type"/>
        <column xsi:type="varchar" name="image" nullable="true" length="255" comment="Image"/>
        <column xsi:type="text" name="text" nullable="true" comment="Text"/>
        <column xsi:type="varchar" name="link" nullable="true" length="255" comment="Link"/>
        <column xsi:type="boolean" name="rel_no_follow" nullable="false" comment="Rel No Follow"/>
        <column xsi:type="boolean" name="status" nullable="false" comment="Status"/>
        <constraint xsi:type="primary" referenceId="PRIMARY">
            <column name="banner_id"/>
        </constraint>
        <index referenceId="AMASTY_AFFILIATE_BANNER_BANNER_ID" indexType="btree">
            <column name="banner_id"/>
        </index>
    </table>
    <table name="amasty_affiliate_lifetime" resource="default" engine="innodb" comment="amasty_affiliate_lifetime">
        <column xsi:type="int" name="lifetime_id" padding="10" unsigned="true" nullable="false" identity="true"
                comment="Lifetime Id"/>
        <column xsi:type="int" name="affiliate_account_id" padding="10" unsigned="true" nullable="true" identity="false"
                comment="Affiliate Account Id"/>
        <column xsi:type="int" name="program_id" padding="10" unsigned="true" nullable="false" identity="false"
                comment="Program Id"/>
        <column xsi:type="varchar" name="customer_email" nullable="false" length="255" comment="Customer Email"/>
        <constraint xsi:type="primary" referenceId="PRIMARY">
            <column name="lifetime_id"/>
        </constraint>
        <constraint xsi:type="foreign" referenceId="FK_F1D971139C2FBEA46BD379A23415AB23"
                    table="amasty_affiliate_lifetime" column="affiliate_account_id"
                    referenceTable="amasty_affiliate_account" referenceColumn="account_id" onDelete="CASCADE"/>
        <constraint xsi:type="foreign" referenceId="FK_59F98076397B2114899B6212EF53E5AD"
                    table="amasty_affiliate_lifetime" column="program_id" referenceTable="amasty_affiliate_program"
                    referenceColumn="program_id" onDelete="CASCADE"/>
        <index referenceId="AMASTY_AFFILIATE_LIFETIME_LIFETIME_ID" indexType="btree">
            <column name="lifetime_id"/>
        </index>
    </table>
    <table name="amasty_affiliate_links" resource="default" engine="innodb" comment="amasty_affiliate_links">
        <column xsi:type="int" name="link_id" padding="10" unsigned="true" nullable="false" identity="true"
                comment="Link Id"/>
        <column xsi:type="int" name="affiliate_account_id" padding="10" unsigned="true" nullable="false"
                identity="false" comment="Affiliate Account Id"/>
        <column xsi:type="timestamp" name="created_at" on_update="false" nullable="false" default="CURRENT_TIMESTAMP"
                comment="Created At"/>
        <column xsi:type="varchar" name="link_type" nullable="true" length="255" comment="Link Type"/>
        <column xsi:type="int" name="element_id" padding="10" unsigned="true" nullable="true" identity="false"
                comment="Element Id"/>
        <constraint xsi:type="primary" referenceId="PRIMARY">
            <column name="link_id"/>
        </constraint>
        <constraint xsi:type="foreign" referenceId="FK_7CA5D6D0D9E068E796CEC77A586D763D" table="amasty_affiliate_links"
                    column="affiliate_account_id" referenceTable="amasty_affiliate_account" referenceColumn="account_id"
                    onDelete="CASCADE"/>
        <index referenceId="AMASTY_AFFILIATE_LINKS_LINK_ID" indexType="btree">
            <column name="link_id"/>
        </index>
    </table>
    <table name="amasty_affiliate_program" resource="default" engine="innodb" comment="amasty_affiliate_program">
        <column xsi:type="int" name="program_id" padding="10" unsigned="true" nullable="false" identity="true"
                comment="Program Id"/>
        <column xsi:type="int" name="rule_id" padding="10" unsigned="true" nullable="true" identity="false"
                comment="Rule Id"/>
        <column xsi:type="varchar" name="name" nullable="true" length="255" comment="Name"/>
        <column xsi:type="smallint" name="affiliate_mode" padding="6" unsigned="false" nullable="false" identity="false"
                comment="Affiliate Mode"/>
        <column xsi:type="varchar" name="withdrawal_type" nullable="false" length="255" comment="Withdrawal Type"/>
        <column xsi:type="boolean" name="is_active" nullable="false" comment="Is Active"/>
        <column xsi:type="decimal" name="commission_value" scale="2" precision="12" unsigned="true" nullable="false"
                default="0" comment="Commission Value" onCreate="migrateDataFrom(commission_value)"/>
        <column xsi:type="decimal" name="commission_per_profit_amount" scale="2" precision="12" unsigned="true"
                nullable="false" default="0" onCreate="migrateDataFrom(commission_per_profit_amount)"/>
        <column xsi:type="varchar" name="commission_value_type" nullable="true" length="255"
                comment="Commission Value Type"/>
        <column xsi:type="boolean" name="from_second_order" nullable="false" comment="From Second Order"/>
        <column xsi:type="varchar" name="commission_type_second" nullable="true" length="255"
                comment="Commission Type Second"/>
        <column xsi:type="decimal" name="commission_value_second" scale="2" precision="12" unsigned="true"
                nullable="false" default="0" comment="Commission Value Second" onCreate="migrateDataFrom(commission_value_second)"/>
        <column xsi:type="boolean" name="is_lifetime" nullable="false" comment="Is Lifetime"/>
        <column xsi:type="varchar" name="frequency" nullable="true" length="255" comment="Frequency"/>
        <column xsi:type="decimal" name="total_sales" scale="2" precision="12" unsigned="true" nullable="false"
                default="0" comment="Total Sales" onCreate="migrateDataFrom(total_sales)"/>
        <column xsi:type="text" name="available_customers" nullable="true" comment="Available Customers Ids"/>
        <column xsi:type="text" name="available_groups" nullable="true" comment="Available users groups"/>
        <column xsi:type="int" name="restrict_transactions_to_number_orders" padding="10" unsigned="true"
                nullable="false" identity="false" default="0" comment="Restrict Transactions To Number Orders"/>
        <constraint xsi:type="primary" referenceId="PRIMARY">
            <column name="program_id"/>
        </constraint>
        <constraint xsi:type="foreign" referenceId="AMASTY_AFFILIATE_PROGRAM_RULE_ID_SALESRULE_RULE_ID"
                    table="amasty_affiliate_program" column="rule_id" referenceTable="salesrule"
                    referenceColumn="rule_id" onDelete="SET NULL"/>
        <index referenceId="AMASTY_AFFILIATE_PROGRAM_PROGRAM_ID" indexType="btree">
            <column name="program_id"/>
        </index>
    </table>
    <table name="amasty_affiliate_coupon" resource="default" engine="innodb" comment="amasty_affiliate_coupon">
        <column xsi:type="int" name="entity_id" padding="10" unsigned="true" nullable="false" identity="true"
                comment="Entity Id"/>
        <column xsi:type="int" name="account_id" padding="10" unsigned="true" nullable="false" identity="false"
                comment="Account Id"/>
        <column xsi:type="int" name="program_id" padding="10" unsigned="true" nullable="false" identity="false"
                comment="Program Id"/>
        <column xsi:type="int" name="coupon_id" padding="10" unsigned="true" nullable="false" identity="false"
                comment="Coupon Id"/>
        <column xsi:type="decimal" name="current_profit" scale="2" precision="12" unsigned="true" nullable="false"
                default="0" comment="Current Profit" onCreate="migrateDataFrom(current_profit)"/>
        <column xsi:type="boolean" name="is_system" nullable="false" default="true" comment="Coupon is system"/>
        <constraint xsi:type="primary" referenceId="PRIMARY">
            <column name="entity_id"/>
        </constraint>
        <constraint xsi:type="foreign" referenceId="FK_6FEF863BBC7D0066999A3F5E0CC92039" table="amasty_affiliate_coupon"
                    column="account_id" referenceTable="amasty_affiliate_account" referenceColumn="account_id"
                    onDelete="CASCADE"/>
        <constraint xsi:type="foreign" referenceId="FK_5ADB7D70F4F4B46C96E39C8C486CDD3B"
                    table="amasty_affiliate_coupon" column="program_id" referenceTable="amasty_affiliate_program"
                    referenceColumn="program_id" onDelete="CASCADE"/>
        <index referenceId="AMASTY_AFFILIATE_COUPON_ACCOUNT_ID" indexType="btree">
            <column name="entity_id"/>
        </index>
    </table>
    <table name="amasty_affiliate_program_order_counter" resource="default" engine="innodb"
           comment="Amasty Affiliate Program Order Counter Table">
        <column xsi:type="int" name="program_id" padding="10" unsigned="true" nullable="false" identity="false"
                comment="Program Id"/>
        <column xsi:type="int" name="affiliate_account_id" padding="10" unsigned="true" nullable="false"
                identity="false" comment="Affiliate Account Id"/>
        <column xsi:type="int" name="order_counter" padding="10" unsigned="true" nullable="false" identity="false"
                default="0" comment="Order Counter"/>
        <constraint xsi:type="primary" referenceId="PRIMARY">
            <column name="program_id"/>
            <column name="affiliate_account_id"/>
        </constraint>
        <constraint xsi:type="foreign" referenceId="FK_0C5542C8A9FE962C8A307E96CF77B6C3"
                    table="amasty_affiliate_program_order_counter" column="program_id"
                    referenceTable="amasty_affiliate_program" referenceColumn="program_id" onDelete="CASCADE"/>
        <constraint xsi:type="foreign" referenceId="FK_9C5B3275953F5FC08AA9336D1AD1285D"
                    table="amasty_affiliate_program_order_counter" column="affiliate_account_id"
                    referenceTable="amasty_affiliate_account" referenceColumn="account_id" onDelete="CASCADE"/>
    </table>
    <table name="amasty_affiliate_commission_calculation" resource="default" engine="innodb"
           comment="Amasty Affiliate Program Commission Calculation Table">
        <column xsi:type="int" name="id" padding="10" unsigned="true" nullable="false" identity="true"
                comment="Entity ID"/>
        <column xsi:type="int" name="program_id" padding="10" unsigned="true" nullable="false" identity="false"
                comment="Program ID"/>
        <column xsi:type="boolean" name="is_enabled" nullable="false" comment="Is Enabled"/>
        <column xsi:type="smallint" name="action_strategy" padding="5" unsigned="true" nullable="true" identity="false"
                comment="Action Strategy Type"/>
        <column xsi:type="text" name="skus" nullable="true" comment="List of Selected SKU's"/>
        <column xsi:type="text" name="categories" nullable="true" comment="List of Selected Categories"/>
        <constraint xsi:type="primary" referenceId="PRIMARY">
            <column name="id"/>
        </constraint>
        <constraint xsi:type="foreign" referenceId="FK_ADA1D9245BB85327B7A1FF2B73010746"
                    table="amasty_affiliate_commission_calculation" column="program_id"
                    referenceTable="amasty_affiliate_program" referenceColumn="program_id" onDelete="CASCADE"/>
    </table>
</schema>
