<?xml version="1.0"?>
<!--
/**
 * @author Amasty Team
 * @copyright Copyright (c) Amasty (https://www.amasty.com)
 * @package Dropshipping 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_perm_role" resource="default" engine="innodb" comment="Amasty Dropshipping Role">
    <column xsi:type="smallint" name="entity_id" padding="5" unsigned="true" nullable="false" identity="true" comment="Entity ID"/>
    <column xsi:type="int" name="role_id" padding="10" unsigned="true" nullable="false" identity="false" default="0" comment="Role ID"/>
    <constraint xsi:type="primary" referenceId="PRIMARY">
      <column name="entity_id"/>
    </constraint>
    <constraint xsi:type="foreign" referenceId="AMASTY_PERM_ROLE_ROLE_ID_AUTHORIZATION_ROLE_ROLE_ID" table="amasty_perm_role" column="role_id" referenceTable="authorization_role" referenceColumn="role_id" onDelete="CASCADE"/>
  </table>
  <table name="amasty_perm_dealer" resource="default" engine="innodb" comment="Amasty Dropshipping Dealer">
    <column xsi:type="int" name="entity_id" padding="10" unsigned="true" nullable="false" identity="true" comment="Entity ID"/>
    <column xsi:type="smallint" name="role_id" padding="5" unsigned="true" nullable="true" identity="false" comment="Role ID"/>
    <column xsi:type="int" name="user_id" padding="10" unsigned="true" nullable="false" identity="false" default="0" comment="User ID"/>
    <column xsi:type="mediumtext" name="description" nullable="true" comment="Description"/>
    <column xsi:type="varchar" name="emails" nullable="true" length="255" comment="Emails"/>
    <column xsi:type="varchar" name="customer_group_ids" nullable="true" length="255" comment="Customer Groups Ids"/>
    <constraint xsi:type="primary" referenceId="PRIMARY">
      <column name="entity_id"/>
    </constraint>
    <constraint xsi:type="foreign" referenceId="AMASTY_PERM_DEALER_ROLE_ID_AMASTY_PERM_ROLE_ENTITY_ID" table="amasty_perm_dealer" column="role_id" referenceTable="amasty_perm_role" referenceColumn="entity_id" onDelete="SET NULL"/>
    <constraint xsi:type="foreign" referenceId="AMASTY_PERM_DEALER_USER_ID_ADMIN_USER_USER_ID" table="amasty_perm_dealer" column="user_id" referenceTable="admin_user" referenceColumn="user_id" onDelete="CASCADE"/>
    <constraint xsi:type="unique" referenceId="AMASTY_PERM_DEALER_USER_ID">
      <column name="user_id"/>
    </constraint>
  </table>
  <table name="amasty_perm_dealer_customer" resource="default" engine="innodb" comment="Amasty Dropshipping Dealer Customer">
    <column xsi:type="int" name="entity_id" padding="10" unsigned="true" nullable="false" identity="true" comment="Entity ID"/>
    <column xsi:type="int" name="dealer_id" padding="10" unsigned="true" nullable="false" identity="false" default="0" comment="Dealer User ID"/>
    <column xsi:type="int" name="customer_id" padding="10" unsigned="true" nullable="false" identity="false" comment="Customer ID"/>
    <constraint xsi:type="primary" referenceId="PRIMARY">
      <column name="entity_id"/>
    </constraint>
    <constraint xsi:type="foreign" referenceId="AMASTY_PERM_DEALER_CSTR_DEALER_ID_AMASTY_PERM_DEALER_ENTT_ID" table="amasty_perm_dealer_customer" column="dealer_id" referenceTable="amasty_perm_dealer" referenceColumn="entity_id" onDelete="CASCADE"/>
    <constraint xsi:type="foreign" referenceId="AMASTY_PERM_DEALER_CSTR_CSTR_ID_CSTR_ENTT_ENTT_ID" table="amasty_perm_dealer_customer" column="customer_id" referenceTable="customer_entity" referenceColumn="entity_id" onDelete="CASCADE"/>
    <constraint xsi:type="unique" referenceId="AMASTY_PERM_DEALER_CUSTOMER_DEALER_ID_CUSTOMER_ID">
      <column name="dealer_id"/>
      <column name="customer_id"/>
    </constraint>
  </table>
  <table name="amasty_perm_dealer_order" resource="default" engine="innodb" comment="Amasty Dropshipping Dealer Order">
    <column xsi:type="int" name="entity_id" padding="10" unsigned="true" nullable="false" identity="true" comment="Entity ID"/>
    <column xsi:type="int" name="dealer_id" padding="10" unsigned="true" nullable="true" identity="false" comment="Dealer User ID"/>
    <column xsi:type="int" name="order_id" padding="10" unsigned="true" nullable="false" identity="false" default="0" comment="Order Id"/>
    <column xsi:type="varchar" name="contactname" nullable="true" length="255" comment="Contact Name"/>
    <constraint xsi:type="primary" referenceId="PRIMARY">
      <column name="entity_id"/>
    </constraint>
    <constraint xsi:type="foreign" referenceId="AMASTY_PERM_DEALER_ORDER_DEALER_ID_AMASTY_PERM_DEALER_ENTITY_ID" table="amasty_perm_dealer_order" column="dealer_id" referenceTable="amasty_perm_dealer" referenceColumn="entity_id" onDelete="CASCADE"/>
    <constraint xsi:type="foreign" referenceId="AMASTY_PERM_DEALER_ORDER_ORDER_ID_SALES_ORDER_ENTITY_ID" table="amasty_perm_dealer_order" column="order_id" referenceTable="sales_order" referenceColumn="entity_id" onDelete="CASCADE"/>
    <constraint xsi:type="unique" referenceId="AMASTY_PERM_DEALER_ORDER_DEALER_ID_ORDER_ID">
      <column name="dealer_id"/>
      <column name="order_id"/>
    </constraint>
  </table>
  <table name="amasty_perm_dealer_order_assign_history" resource="default" engine="innodb" comment="Amasty Dropshipping Dealer Order Assign History">
    <column xsi:type="int" name="entity_id" padding="10" unsigned="true" nullable="false" identity="true" comment="Entity Id"/>
    <column xsi:type="int" name="parent_id" padding="10" unsigned="true" nullable="false" identity="false" comment="Parent Id"/>
    <column xsi:type="int" name="is_dealer_notified" padding="11" unsigned="false" nullable="true" identity="false" comment="Is Dealer Notified"/>
    <column xsi:type="text" name="comment" nullable="true" comment="Comment"/>
    <column xsi:type="int" name="author_dealer_id" padding="10" unsigned="true" nullable="true" identity="false" comment="Author Dealer ID"/>
    <column xsi:type="varchar" name="author_dealer_contactname" nullable="true" length="255" comment="Author Dealer Contact Name"/>
    <column xsi:type="int" name="from_dealer_id" padding="10" unsigned="true" nullable="true" identity="false" comment="From Dealer ID"/>
    <column xsi:type="varchar" name="from_dealer_contactname" nullable="true" length="255" comment="From Dealer Contact Name"/>
    <column xsi:type="int" name="to_dealer_id" padding="10" unsigned="true" nullable="true" identity="false" comment="To Dealer ID"/>
    <column xsi:type="varchar" name="to_dealer_contactname" nullable="true" length="255" comment="To Dealer Contact Name"/>
    <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="entity_id"/>
    </constraint>
    <constraint xsi:type="foreign" referenceId="FK_49358C2B456B3DC5CDB2B24963A295E3" table="amasty_perm_dealer_order_assign_history" column="parent_id" referenceTable="sales_order" referenceColumn="entity_id" onDelete="CASCADE"/>
    <index referenceId="AMASTY_PERM_DEALER_ORDER_PARENT_ID" indexType="btree">
      <column name="parent_id"/>
    </index>
    <index referenceId="AMASTY_PERM_DEALER_ORDER_CREATED_AT" indexType="btree">
      <column name="created_at"/>
    </index>
  </table>
  <table name="amasty_perm_dealer_order_item" resource="default" engine="innodb" comment="Amasty Dropshipping Dealer Order Item Table">
    <column xsi:type="int" name="entity_id" padding="10" unsigned="true" nullable="false" identity="true" comment="Entity ID"/>
    <column xsi:type="int" name="dealer_id" padding="10" unsigned="true" nullable="true" identity="false" comment="Dealer User ID"/>
    <column xsi:type="int" name="order_item_id" padding="10" unsigned="true" nullable="false" identity="false" default="0" comment="Order Item Id"/>
    <column xsi:type="varchar" name="notified" nullable="false" length="255" default="no" comment="Notified"/>
    <constraint xsi:type="primary" referenceId="PRIMARY">
      <column name="entity_id"/>
    </constraint>
    <constraint xsi:type="foreign" referenceId="FK_7750A0C83967E502A3296140BDDC9A44" table="amasty_perm_dealer_order_item" column="order_item_id" referenceTable="sales_order_item" referenceColumn="item_id" onDelete="CASCADE"/>
  </table>
</schema>
