<?xml version="1.0"?>
<schema xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:noNamespaceSchemaLocation="urn:magento:framework:Setup/Declaration/Schema/etc/schema.xsd">
    <table name="liveprawn_condo_group" resource="default" engine="innodb"
           comment="Live Prawn Condo Group Buy Groups">
        <column xsi:type="int" name="entity_id" unsigned="true" nullable="false" identity="true"
                comment="Entity ID"/>
        <column xsi:type="varchar" name="condo_name" length="255" nullable="false"
                comment="Condo Name"/>
        <column xsi:type="varchar" name="slug" length="128" nullable="false"
                comment="URL Slug"/>
        <column xsi:type="varchar" name="address_line_1" length="255" nullable="false"
                comment="Address Line 1"/>
        <column xsi:type="varchar" name="address_line_2" length="255" nullable="true"
                comment="Address Line 2"/>
        <column xsi:type="varchar" name="city" length="128" nullable="false"
                comment="City"/>
        <column xsi:type="varchar" name="postcode" length="16" nullable="false"
                comment="Postcode"/>
        <column xsi:type="varchar" name="state" length="64" nullable="false"
                comment="State"/>
        <column xsi:type="varchar" name="country" length="2" nullable="false" default="MY"
                comment="Country"/>
        <column xsi:type="varchar" name="collection_point" length="255" nullable="false"
                comment="Collection Point"/>
        <column xsi:type="date" name="delivery_day" nullable="true"
                comment="Delivery Day"/>
        <column xsi:type="varchar" name="delivery_time_window" length="64" nullable="true"
                comment="Delivery Time Window"/>
        <column xsi:type="datetime" name="order_cutoff_datetime" nullable="true"
                comment="Order Cutoff Datetime"/>
        <column xsi:type="decimal" name="minimum_group_kg" scale="2" precision="8" unsigned="false"
                nullable="true" comment="Minimum Group KG"/>
        <column xsi:type="varchar" name="status" length="16" nullable="false" default="inactive"
                comment="Status"/>
        <column xsi:type="varchar" name="shipping_fee_type" length="16" nullable="false" default="fixed_fee"
                comment="Shipping Fee Type"/>
        <column xsi:type="decimal" name="shipping_fee_amount" scale="4" precision="12" unsigned="false"
                nullable="false" default="4.0000" comment="Shipping Fee Amount"/>
        <column xsi:type="text" name="notes" nullable="true" comment="Notes"/>
        <column xsi:type="int" name="distributor_customer_id" unsigned="true" nullable="true"
                comment="Assigned Distributor Customer ID"/>
        <column xsi:type="varchar" name="distributor_tier" length="16" nullable="true"
                comment="Distributor Tier"/>
        <column xsi:type="smallint" name="distributor_commission_active" unsigned="true" nullable="false"
                default="0" comment="Distributor Commission Active"/>
        <column xsi:type="text" name="distributor_commission_note" nullable="true"
                comment="Distributor Commission Note"/>
        <column xsi:type="timestamp" name="created_at" nullable="false" default="CURRENT_TIMESTAMP"
                comment="Created At"/>
        <column xsi:type="timestamp" name="updated_at" nullable="false" default="CURRENT_TIMESTAMP"
                on_update="true" comment="Updated At"/>
        <constraint xsi:type="primary" referenceId="PRIMARY">
            <column name="entity_id"/>
        </constraint>
        <constraint xsi:type="unique" referenceId="LIVEPRAWN_CONDO_GROUP_SLUG">
            <column name="slug"/>
        </constraint>
        <index referenceId="LIVEPRAWN_CONDO_GROUP_STATUS" indexType="btree">
            <column name="status"/>
        </index>
        <index referenceId="LIVEPRAWN_CONDO_GROUP_DELIVERY_DAY" indexType="btree">
            <column name="delivery_day"/>
        </index>
        <index referenceId="LIVEPRAWN_CONDO_GROUP_ORDER_CUTOFF" indexType="btree">
            <column name="order_cutoff_datetime"/>
        </index>
        <index referenceId="LIVEPRAWN_CONDO_GROUP_DISTRIBUTOR_CUSTOMER_ID" indexType="btree">
            <column name="distributor_customer_id"/>
        </index>
    </table>

    <table name="liveprawn_condo_product_map" resource="default" engine="innodb"
           comment="Live Prawn Condo Group Buy Product Matrix Map">
        <column xsi:type="int" name="entity_id" unsigned="true" nullable="false" identity="true"
                comment="Entity ID"/>
        <column xsi:type="varchar" name="prawn_type" length="16" nullable="false"
                comment="Prawn Type"/>
        <column xsi:type="varchar" name="product_state" length="16" nullable="false"
                comment="Product State"/>
        <column xsi:type="varchar" name="size_band" length="16" nullable="false"
                comment="Size Band"/>
        <column xsi:type="int" name="product_id" unsigned="true" nullable="false"
                comment="Catalog Product ID"/>
        <column xsi:type="varchar" name="sku_snapshot" length="64" nullable="false"
                comment="SKU Snapshot"/>
        <column xsi:type="varchar" name="display_label" length="128" nullable="false"
                comment="Display Label"/>
        <column xsi:type="int" name="sort_order" unsigned="true" nullable="false" default="0"
                comment="Sort Order"/>
        <column xsi:type="varchar" name="status" length="16" nullable="false" default="active"
                comment="Status"/>
        <column xsi:type="timestamp" name="created_at" nullable="false" default="CURRENT_TIMESTAMP"
                comment="Created At"/>
        <column xsi:type="timestamp" name="updated_at" nullable="false" default="CURRENT_TIMESTAMP"
                on_update="true" comment="Updated At"/>
        <constraint xsi:type="primary" referenceId="PRIMARY">
            <column name="entity_id"/>
        </constraint>
        <constraint xsi:type="unique" referenceId="LIVEPRAWN_CONDO_PRODUCT_MAP_MATRIX_KEY">
            <column name="prawn_type"/>
            <column name="product_state"/>
            <column name="size_band"/>
        </constraint>
        <constraint xsi:type="foreign" referenceId="LIVEPRAWN_CONDO_PRODUCT_MAP_PRODUCT_ID"
                    table="liveprawn_condo_product_map" column="product_id"
                    referenceTable="catalog_product_entity" referenceColumn="entity_id"
                    onDelete="CASCADE"/>
        <index referenceId="LIVEPRAWN_CONDO_PRODUCT_MAP_PRODUCT_ID" indexType="btree">
            <column name="product_id"/>
        </index>
        <index referenceId="LIVEPRAWN_CONDO_PRODUCT_MAP_STATUS" indexType="btree">
            <column name="status"/>
        </index>
        <index referenceId="LIVEPRAWN_CONDO_PRODUCT_MAP_SORT_ORDER" indexType="btree">
            <column name="sort_order"/>
        </index>
    </table>

    <table name="liveprawn_condo_group_product" resource="default" engine="innodb"
           comment="Live Prawn Condo Group Buy Per-Condo Product Assignment">
        <column xsi:type="int" name="entity_id" unsigned="true" nullable="false" identity="true"
                comment="Entity ID"/>
        <column xsi:type="int" name="condo_group_id" unsigned="true" nullable="false"
                comment="Condo Group ID"/>
        <column xsi:type="int" name="product_id" unsigned="true" nullable="false"
                comment="Catalog Product ID"/>
        <column xsi:type="varchar" name="sku_snapshot" length="64" nullable="false"
                comment="SKU Snapshot"/>
        <column xsi:type="varchar" name="product_name_snapshot" length="255" nullable="false"
                comment="Product Name Snapshot"/>
        <column xsi:type="varchar" name="display_label" length="255" nullable="false"
                comment="Display Label"/>
        <column xsi:type="decimal" name="custom_price_override" scale="4" precision="12"
                unsigned="false" nullable="true" comment="Custom Price Override"/>
        <column xsi:type="text" name="qty_options" nullable="true"
                comment="Qty Options JSON"/>
        <column xsi:type="decimal" name="min_qty" scale="4" precision="12" unsigned="false"
                nullable="false" default="1" comment="Minimum Qty"/>
        <column xsi:type="int" name="sort_order" unsigned="true" nullable="false" default="0"
                comment="Sort Order"/>
        <column xsi:type="varchar" name="status" length="16" nullable="false" default="active"
                comment="Status"/>
        <column xsi:type="timestamp" name="created_at" nullable="false" default="CURRENT_TIMESTAMP"
                comment="Created At"/>
        <column xsi:type="timestamp" name="updated_at" nullable="false" default="CURRENT_TIMESTAMP"
                on_update="true" comment="Updated At"/>
        <constraint xsi:type="primary" referenceId="PRIMARY">
            <column name="entity_id"/>
        </constraint>
        <constraint xsi:type="unique" referenceId="LIVEPRAWN_CONDO_GROUP_PRODUCT_UNIQUE">
            <column name="condo_group_id"/>
            <column name="product_id"/>
        </constraint>
        <constraint xsi:type="foreign" referenceId="LIVEPRAWN_CONDO_GROUP_PRODUCT_CONDO_GROUP_ID"
                    table="liveprawn_condo_group_product" column="condo_group_id"
                    referenceTable="liveprawn_condo_group" referenceColumn="entity_id"
                    onDelete="CASCADE"/>
        <constraint xsi:type="foreign" referenceId="LIVEPRAWN_CONDO_GROUP_PRODUCT_PRODUCT_ID"
                    table="liveprawn_condo_group_product" column="product_id"
                    referenceTable="catalog_product_entity" referenceColumn="entity_id"
                    onDelete="CASCADE"/>
        <index referenceId="LIVEPRAWN_CONDO_GROUP_PRODUCT_CONDO_GROUP_ID" indexType="btree">
            <column name="condo_group_id"/>
        </index>
        <index referenceId="LIVEPRAWN_CONDO_GROUP_PRODUCT_STATUS" indexType="btree">
            <column name="status"/>
        </index>
        <index referenceId="LIVEPRAWN_CONDO_GROUP_PRODUCT_SORT_ORDER" indexType="btree">
            <column name="sort_order"/>
        </index>
    </table>

    <table name="liveprawn_condo_group_image" resource="default" engine="innodb"
           comment="Live Prawn Condo Group Buy Promo Images">
        <column xsi:type="int" name="entity_id" unsigned="true" nullable="false" identity="true"
                comment="Entity ID"/>
        <column xsi:type="int" name="condo_group_id" unsigned="true" nullable="false"
                comment="Condo Group ID"/>
        <column xsi:type="varchar" name="image_path" length="512" nullable="false"
                comment="Image Path Relative To Media"/>
        <column xsi:type="varchar" name="title" length="255" nullable="true"
                comment="Image Title"/>
        <column xsi:type="text" name="caption" nullable="true"
                comment="Image Caption"/>
        <column xsi:type="int" name="sort_order" unsigned="true" nullable="false" default="0"
                comment="Sort Order"/>
        <column xsi:type="varchar" name="status" length="16" nullable="false" default="active"
                comment="Status"/>
        <column xsi:type="timestamp" name="created_at" nullable="false" default="CURRENT_TIMESTAMP"
                comment="Created At"/>
        <column xsi:type="timestamp" name="updated_at" nullable="false" default="CURRENT_TIMESTAMP"
                on_update="true" comment="Updated At"/>
        <constraint xsi:type="primary" referenceId="PRIMARY">
            <column name="entity_id"/>
        </constraint>
        <constraint xsi:type="foreign" referenceId="LIVEPRAWN_CONDO_GROUP_IMAGE_CONDO_GROUP_ID"
                    table="liveprawn_condo_group_image" column="condo_group_id"
                    referenceTable="liveprawn_condo_group" referenceColumn="entity_id"
                    onDelete="CASCADE"/>
        <index referenceId="LIVEPRAWN_CONDO_GROUP_IMAGE_CONDO_GROUP_ID" indexType="btree">
            <column name="condo_group_id"/>
        </index>
        <index referenceId="LIVEPRAWN_CONDO_GROUP_IMAGE_STATUS" indexType="btree">
            <column name="status"/>
        </index>
        <index referenceId="LIVEPRAWN_CONDO_GROUP_IMAGE_SORT_ORDER" indexType="btree">
            <column name="sort_order"/>
        </index>
    </table>

    <table name="quote" resource="checkout" comment="Quote">
        <column xsi:type="smallint" name="is_condo_group_buy" unsigned="true" nullable="false"
                default="0" comment="Is Condo Group Buy"/>
        <column xsi:type="int" name="condo_group_id" unsigned="true" nullable="true"
                comment="Condo Group ID"/>
        <column xsi:type="varchar" name="condo_name_snapshot" length="255" nullable="true"
                comment="Condo Name Snapshot"/>
        <column xsi:type="varchar" name="condo_unit_number" length="64" nullable="true"
                comment="Condo Unit Number"/>
        <column xsi:type="varchar" name="condo_buyer_name" length="100" nullable="true"
                comment="Condo Buyer Name"/>
        <column xsi:type="varchar" name="condo_buyer_phone" length="30" nullable="true"
                comment="Condo Buyer Phone"/>
        <column xsi:type="varchar" name="condo_collection_point_snapshot" length="255" nullable="true"
                comment="Condo Collection Point Snapshot"/>
        <column xsi:type="date" name="condo_delivery_batch_date" nullable="true"
                comment="Condo Delivery Batch Date"/>
    </table>

    <table name="sales_order" resource="sales" comment="Sales Order">
        <column xsi:type="smallint" name="is_condo_group_buy" unsigned="true" nullable="false"
                default="0" comment="Is Condo Group Buy"/>
        <column xsi:type="int" name="condo_group_id" unsigned="true" nullable="true"
                comment="Condo Group ID"/>
        <column xsi:type="varchar" name="condo_name_snapshot" length="255" nullable="true"
                comment="Condo Name Snapshot"/>
        <column xsi:type="varchar" name="condo_unit_number" length="64" nullable="true"
                comment="Condo Unit Number"/>
        <column xsi:type="varchar" name="condo_buyer_name" length="100" nullable="true"
                comment="Condo Buyer Name"/>
        <column xsi:type="varchar" name="condo_buyer_phone" length="30" nullable="true"
                comment="Condo Buyer Phone"/>
        <column xsi:type="varchar" name="condo_collection_point_snapshot" length="255" nullable="true"
                comment="Condo Collection Point Snapshot"/>
        <column xsi:type="date" name="condo_delivery_batch_date" nullable="true"
                comment="Condo Delivery Batch Date"/>
    </table>

    <table name="liveprawn_condo_group_order" resource="default" engine="innodb"
           comment="Live Prawn Condo Group Buy Order Snapshot">
        <column xsi:type="int" name="entity_id" unsigned="true" nullable="false" identity="true"
                comment="Entity ID"/>
        <column xsi:type="int" name="condo_group_id" unsigned="true" nullable="false"
                comment="Condo Group ID"/>
        <column xsi:type="int" name="order_id" unsigned="true" nullable="true"
                comment="Sales Order ID"/>
        <column xsi:type="int" name="quote_id" unsigned="true" nullable="false"
                comment="Quote ID"/>
        <column xsi:type="varchar" name="order_increment_id" length="32" nullable="true"
                comment="Order Increment ID"/>
        <column xsi:type="varchar" name="condo_name_snapshot" length="255" nullable="false"
                comment="Condo Name Snapshot"/>
        <column xsi:type="varchar" name="unit_number" length="64" nullable="false"
                comment="Unit Number"/>
        <column xsi:type="varchar" name="buyer_name" length="100" nullable="false"
                comment="Buyer Name"/>
        <column xsi:type="varchar" name="buyer_phone" length="30" nullable="false"
                comment="Buyer Phone"/>
        <column xsi:type="varchar" name="collection_point_snapshot" length="255" nullable="false"
                comment="Collection Point Snapshot"/>
        <column xsi:type="date" name="delivery_batch_date" nullable="true"
                comment="Delivery Batch Date"/>
        <column xsi:type="text" name="address_snapshot" nullable="true"
                comment="Address Snapshot"/>
        <column xsi:type="varchar" name="status" length="16" nullable="false" default="pending"
                comment="Status"/>
        <column xsi:type="timestamp" name="created_at" nullable="false" default="CURRENT_TIMESTAMP"
                comment="Created At"/>
        <column xsi:type="timestamp" name="updated_at" nullable="false" default="CURRENT_TIMESTAMP"
                on_update="true" comment="Updated At"/>
        <constraint xsi:type="primary" referenceId="PRIMARY">
            <column name="entity_id"/>
        </constraint>
        <constraint xsi:type="foreign" referenceId="LIVEPRAWN_CONDO_GROUP_ORDER_CONDO_GROUP_ID"
                    table="liveprawn_condo_group_order" column="condo_group_id"
                    referenceTable="liveprawn_condo_group" referenceColumn="entity_id"
                    onDelete="CASCADE"/>
        <constraint xsi:type="unique" referenceId="LIVEPRAWN_CONDO_GROUP_ORDER_QUOTE_ID">
            <column name="quote_id"/>
        </constraint>
        <index referenceId="LIVEPRAWN_CONDO_GROUP_ORDER_ORDER_ID" indexType="btree">
            <column name="order_id"/>
        </index>
        <index referenceId="LIVEPRAWN_CONDO_GROUP_ORDER_STATUS" indexType="btree">
            <column name="status"/>
        </index>
        <index referenceId="LIVEPRAWN_CONDO_GROUP_ORDER_DELIVERY_BATCH" indexType="btree">
            <column name="delivery_batch_date"/>
        </index>
    </table>
</schema>
