<?xml version="1.0"?>
<!--
/**
 * Copyright © Zhik Pty Ltd. All rights reserved.
 * See LICENSE.txt for license details.
 */
-->
<schema xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:noNamespaceSchemaLocation="urn:magento:framework:Setup/Declaration/Schema/etc/schema.xsd">

    <!-- Extend existing saved orders table with order type differentiation -->
    <table name="zhik_portal_saved_orders" resource="default" engine="innodb"
           comment="Portal Saved Orders">

        <!-- Existing columns from InstallSchema.php -->
        <column xsi:type="int" name="id" unsigned="true" nullable="false" identity="true"
                comment="ID"/>
        <column xsi:type="int" name="creator_id" unsigned="true" nullable="false"
                comment="Creator ID"/>
        <column xsi:type="int" name="customer_id" unsigned="true" nullable="false"
                comment="Customer ID"/>
        <column xsi:type="varchar" name="title" nullable="false" length="255"
                comment="Title"/>
        <column xsi:type="text" name="products" nullable="false"
                comment="Products JSON"/>
        <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"/>

        <!-- NEW: Add order_type column to differentiate Portal vs Forward Order saved orders -->
        <column xsi:type="varchar" name="order_type" nullable="true" length="20"
                comment="Order Type (NULL=portal, SF=forward order)"/>

        <!-- Primary Key -->
        <constraint xsi:type="primary" referenceId="PRIMARY">
            <column name="id"/>
        </constraint>

        <!-- Foreign Keys -->
        <constraint xsi:type="foreign" referenceId="ZHIK_PORTAL_SAVED_ORDERS_CREATOR_ID_CUSTOMER_ENTITY_ENTITY_ID"
                    table="zhik_portal_saved_orders" column="creator_id"
                    referenceTable="customer_entity" referenceColumn="entity_id"
                    onDelete="CASCADE"/>
        <constraint xsi:type="foreign" referenceId="ZHIK_PORTAL_SAVED_ORDERS_CUSTOMER_ID_CUSTOMER_ENTITY_ENTITY_ID"
                    table="zhik_portal_saved_orders" column="customer_id"
                    referenceTable="customer_entity" referenceColumn="entity_id"
                    onDelete="CASCADE"/>

        <!-- Indexes -->
        <index referenceId="ZHIK_PORTAL_SAVED_ORDERS_CREATOR_ID" indexType="btree">
            <column name="creator_id"/>
        </index>
        <index referenceId="IDX_PORTAL_SAVED_ORDER_TYPE" indexType="btree">
            <column name="order_type"/>
        </index>
    </table>
</schema>
