<?php
declare(strict_types=1);

return [
    'up' => "
        CREATE TABLE IF NOT EXISTS `vendor_invoices` (
            `id`                  BIGINT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
            `invoice_number`      VARCHAR(100) NOT NULL COMMENT 'Supplier invoice number',
            `internal_number`     VARCHAR(50) NOT NULL COMMENT 'System-generated VINV-YYYY-NNNNNN',
            `supplier_id`         BIGINT UNSIGNED NOT NULL COMMENT 'FK suppliers.id',
            `purchase_order_id`   BIGINT UNSIGNED NULL COMMENT 'FK purchase_orders.id',
            `grn_id`              BIGINT UNSIGNED NULL COMMENT 'FK goods_received_notes.id',
            `invoice_date`        DATE NOT NULL,
            `due_date`            DATE NULL,
            `subtotal`            DECIMAL(18,2) NOT NULL DEFAULT 0.00,
            `tax_amount`          DECIMAL(18,2) NOT NULL DEFAULT 0.00,
            `discount_amount`     DECIMAL(18,2) NOT NULL DEFAULT 0.00,
            `total_amount`        DECIMAL(18,2) NOT NULL DEFAULT 0.00,
            `currency`            VARCHAR(3) NOT NULL DEFAULT 'EGP',
            `status`              VARCHAR(20) NOT NULL DEFAULT 'draft' COMMENT 'draft,verified,approved,paid,partial_paid,cancelled',
            `match_status`        VARCHAR(20) NULL COMMENT 'unmatched,matched,discrepancy,tolerance_pass',
            `match_notes`         TEXT NULL,
            `journal_entry_id`    BIGINT UNSIGNED NULL COMMENT 'FK journal_entries.id',
            `ap_record_id`        BIGINT UNSIGNED NULL COMMENT 'FK accounts_payable.id',
            `notes`               TEXT NULL,
            `branch_id`           BIGINT UNSIGNED NULL,
            `is_archived`         TINYINT(1) NOT NULL DEFAULT 0,
            `archived_at`         DATETIME NULL,
            `archived_by`         BIGINT UNSIGNED NULL,
            `created_at`          DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
            `updated_at`          DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
            `created_by`          BIGINT UNSIGNED NULL,
            `updated_by`          BIGINT UNSIGNED NULL,
            UNIQUE INDEX `uq_vinv_internal` (`internal_number`),
            INDEX `idx_vinv_supplier` (`supplier_id`),
            INDEX `idx_vinv_po` (`purchase_order_id`),
            INDEX `idx_vinv_grn` (`grn_id`),
            INDEX `idx_vinv_status` (`status`),
            INDEX `idx_vinv_match` (`match_status`),
            INDEX `idx_vinv_due_date` (`due_date`)
        ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci
    ",
    'down' => "DROP TABLE IF EXISTS `vendor_invoices`",
];
