<?php
declare(strict_types=1);

return [
    'up' => "
        CREATE TABLE IF NOT EXISTS `purchase_orders` (
            `id`                BIGINT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
            `po_number`         VARCHAR(50) NOT NULL,
            `supplier_id`       BIGINT UNSIGNED NOT NULL,
            `warehouse_id`      BIGINT UNSIGNED NOT NULL,
            `status`            VARCHAR(20) NOT NULL DEFAULT 'draft' COMMENT 'draft, submitted, approved, partially_received, received, cancelled',
            `order_date`        DATE NOT NULL,
            `expected_delivery` DATE NULL,
            `subtotal`          DECIMAL(15,2) NOT NULL DEFAULT 0.00,
            `tax_amount`        DECIMAL(15,2) NOT NULL DEFAULT 0.00,
            `discount_amount`   DECIMAL(15,2) NOT NULL DEFAULT 0.00,
            `total_amount`      DECIMAL(15,2) NOT NULL DEFAULT 0.00,
            `currency`          VARCHAR(3) NOT NULL DEFAULT 'EGP',
            `payment_terms`     VARCHAR(100) NULL,
            `submitted_by`      BIGINT UNSIGNED NULL,
            `submitted_at`      TIMESTAMP NULL DEFAULT NULL,
            `approved_by`       BIGINT UNSIGNED NULL,
            `approved_at`       TIMESTAMP NULL DEFAULT NULL,
            `notes`             TEXT NULL,
            `branch_id`         BIGINT UNSIGNED NULL,
            `is_archived`       TINYINT(1) NOT NULL DEFAULT 0,
            `archived_at`       TIMESTAMP NULL DEFAULT NULL,
            `archived_by`       BIGINT UNSIGNED NULL,
            `created_at`        TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
            `updated_at`        TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
            `created_by`        BIGINT UNSIGNED NULL,
            `updated_by`        BIGINT UNSIGNED NULL,
            UNIQUE KEY `uq_purchase_orders_number` (`po_number`),
            INDEX `idx_po_supplier` (`supplier_id`),
            INDEX `idx_po_warehouse` (`warehouse_id`),
            INDEX `idx_po_status` (`status`),
            INDEX `idx_po_date` (`order_date`),
            CONSTRAINT `fk_po_supplier` FOREIGN KEY (`supplier_id`) REFERENCES `suppliers`(`id`) ON DELETE RESTRICT,
            CONSTRAINT `fk_po_warehouse` FOREIGN KEY (`warehouse_id`) REFERENCES `warehouses`(`id`) ON DELETE RESTRICT
        ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci
    ",
    'down' => "DROP TABLE IF EXISTS `purchase_orders`",
];
