<?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="gti_salesforce_invoices" resource="default" engine="innodb" comment="GTI Salesforce Invoices">
        <column xsi:type="int" name="entity_id" unsigned="true" nullable="false" identity="true" comment="Entity ID"/>
        <column xsi:type="varchar" name="salesforce_account_id" nullable="false" comment="Salesforce Account ID"/>

        <!--    Invoice Header    -->
        <column xsi:type="date" name="date" nullable="false" comment="Date"/>
        <column xsi:type="varchar" name="acomba_invoice_id" nullable="false"
                comment="Acomba Invoice ID (No/ NO)"/>

        <column xsi:type="varchar" name="sales_rep" nullable="true" comment="Vendeur / Sales Rep."/>
        <column xsi:type="varchar" name="shipping_method" nullable="true" comment="Expédieur / Shipping"/>

        <!--    Shipment tracking    -->
        <column xsi:type="varchar" name="carrier" length="255" nullable="true" comment="Carrier"/>
        <column xsi:type="text" name="tracking_url" nullable="true" comment="Tracking URL"/>
        <column xsi:type="date" name="shipped_date" nullable="true" comment="Shipped Date"/>
        <column xsi:type="varchar" name="terms" nullable="true" comment="Termes / Terms"/>
        <column xsi:type="varchar" name="po_number" nullable="true" comment="PO Number (Référence / P.O.)"/>
        <column xsi:type="varchar" name="acomba_order_id" nullable="false"
                comment="Acomba order ID (Commande / Order)"/>
        <column xsi:type="varchar" name="salesforce_order_number" nullable="false"
                comment="Salesforce order number (Ref. Entry / Saisie)"/>

        <!--    Client / Customer    -->
        <column xsi:type="varchar" name="customer_number" nullable="true" comment="Customer Number"/>
        <column xsi:type="text" name="billing_address" nullable="true" comment="Billing Address"/>

        <!--    Ship to    -->
        <column xsi:type="text" name="shipping_address" nullable="true" comment="Billing Address"/>


        <!--Total Section-->
        <column xsi:type="decimal" name="shipping_cost" precision="16" scale="2" nullable="false"
                comment="Shipping Cost"/>

        <column xsi:type="decimal" name="subtotal" precision="16" scale="2" nullable="false" comment="Subtotal"/>

        <column xsi:type="decimal" name="tax_one" precision="16" scale="2" nullable="false" comment="Tax 1"/>
        <column xsi:type="decimal" name="tax_two" precision="16" scale="2" nullable="false" comment="Tax 2"/>

        <column xsi:type="decimal" name="total" precision="16" scale="2" nullable="false" comment="Total"/>

        <column xsi:type="timestamp" name="created_at" on_update="false" nullable="false" default="CURRENT_TIMESTAMP"
                comment="Created At"/>
        <column xsi:type="timestamp" name="updated_at" on_update="true" nullable="false" default="CURRENT_TIMESTAMP"
                comment="Updated At"/>
        <constraint xsi:type="primary" referenceId="PRIMARY">
            <column name="entity_id"/>
        </constraint>

        <index indexType="btree" referenceId="GTI_SALESFORCE_INVOICES_ACOMBA_INVOICE_ID">
            <column name="acomba_invoice_id"/>
        </index>
    </table>

    <table name="gti_salesforce_invoice_lines" resource="default" engine="innodb"
           comment="GTI Salesforce Invoice Lines">
        <column xsi:type="int" name="entity_id" unsigned="true" nullable="false" identity="true" comment="Entity ID"/>

        <!-- NOTE: Removed identity="true" from invoice_id below -->
        <column xsi:type="int" name="invoice_id" unsigned="true" nullable="false" comment="Invoice ID"/>

        <column xsi:type="varchar" name="sku" nullable="false" comment="SKU (ITEM NO.)"/>

        <column xsi:type="text" name="description" nullable="true" comment="Product Name (Description line 1)"/>
        <column xsi:type="varchar" name="serial_number" nullable="true" comment="Serial Number (Description line 2)"/>

        <column xsi:type="varchar" name="sold_in" nullable="true" comment="Sold in / Unit of measure"/>

        <column xsi:type="int" name="quantity_billed" nullable="false" comment="Quantity Billed"/>
        <column xsi:type="decimal" name="unit_cost" precision="16" scale="2" nullable="false" comment="Unit Cost"/>
        <column xsi:type="decimal" name="line_discount" precision="16" scale="2" nullable="true"
                comment="Line Discount(ESC. / DISC.)"/>
        <column xsi:type="decimal" name="line_cost" precision="16" scale="2" nullable="false" comment="Line cost"/>

        <column xsi:type="timestamp" name="created_at" on_update="false" nullable="false" default="CURRENT_TIMESTAMP"
                comment="Created At"/>
        <column xsi:type="timestamp" name="updated_at" on_update="true" nullable="false" default="CURRENT_TIMESTAMP"
                comment="Updated At"/>

        <!-- Primary Key for lines table -->
        <constraint xsi:type="primary" referenceId="PRIMARY">
            <column name="entity_id"/>
        </constraint>

        <!-- Foreign Key Constraint -->
        <constraint
            xsi:type="foreign"
            referenceId="GTI_SALESFORCE_INVOICE_LINES_INVOICE_ID_INVOICES_ENTITY_ID"
            table="gti_salesforce_invoice_lines"
            column="invoice_id"
            referenceTable="gti_salesforce_invoices"
            referenceColumn="entity_id"
            onDelete="CASCADE"/>
    </table>
</schema>
