<?php
declare(strict_types=1);

return [
    'up' => "
        CREATE TABLE IF NOT EXISTS `accounts_payable` (
            `id`                BIGINT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
            `supplier_id`       BIGINT UNSIGNED NOT NULL,
            `invoice_number`    VARCHAR(50)     NOT NULL,
            `invoice_date`      DATE            NOT NULL,
            `due_date`          DATE            NOT NULL,
            `description_ar`    VARCHAR(300)    NOT NULL,
            `description_en`    VARCHAR(300)    NULL,
            `total_amount`      DECIMAL(18,2)   NOT NULL,
            `paid_amount`       DECIMAL(18,2)   NOT NULL DEFAULT 0.00,
            `balance`           DECIMAL(18,2)   NOT NULL DEFAULT 0.00,
            `currency`          VARCHAR(3)      NOT NULL DEFAULT 'EGP',
            `status`            ENUM('pending','partial','paid','overdue','cancelled') NOT NULL DEFAULT 'pending',
            `journal_entry_id`  BIGINT UNSIGNED NULL COMMENT 'Original accrual entry',
            `payment_entry_id`  BIGINT UNSIGNED NULL COMMENT 'Payment journal entry',
            `purchase_order_id` BIGINT UNSIGNED NULL,
            `cost_center_id`    BIGINT UNSIGNED NULL,
            `branch_id`         BIGINT UNSIGNED NULL,
            `notes`             TEXT            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,

            INDEX `idx_ap_supplier`       (`supplier_id`),
            INDEX `idx_ap_status`          (`status`),
            INDEX `idx_ap_due_date`        (`due_date`),
            INDEX `idx_ap_invoice_date`    (`invoice_date`),
            INDEX `idx_ap_cost_center`     (`cost_center_id`),
            INDEX `idx_ap_branch`          (`branch_id`),
            CONSTRAINT `fk_ap_supplier`       FOREIGN KEY (`supplier_id`)       REFERENCES `suppliers`(`id`),
            CONSTRAINT `fk_ap_journal_entry`  FOREIGN KEY (`journal_entry_id`)  REFERENCES `journal_entries`(`id`),
            CONSTRAINT `fk_ap_payment_entry`  FOREIGN KEY (`payment_entry_id`)  REFERENCES `journal_entries`(`id`),
            CONSTRAINT `fk_ap_purchase_order` FOREIGN KEY (`purchase_order_id`) REFERENCES `purchase_orders`(`id`),
            CONSTRAINT `fk_ap_cost_center`    FOREIGN KEY (`cost_center_id`)    REFERENCES `cost_centers`(`id`),
            CONSTRAINT `fk_ap_branch`         FOREIGN KEY (`branch_id`)         REFERENCES `branches`(`id`),
            CONSTRAINT `fk_ap_created_by`     FOREIGN KEY (`created_by`)        REFERENCES `employees`(`id`)
        ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci
    ",
    'down' => "DROP TABLE IF EXISTS `accounts_payable`",
];
