<?php
declare(strict_types=1);

return [
    'up' => "
        CREATE TABLE IF NOT EXISTS `journal_entries` (
            `id`                  BIGINT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
            `entry_number`        VARCHAR(30)     NOT NULL COMMENT 'JE-2026-000001',
            `fiscal_year_id`      BIGINT UNSIGNED NOT NULL,
            `entry_date`          DATE            NOT NULL,
            `reference_type`      VARCHAR(50)     NULL COMMENT 'payment, sale, payroll, subscription, fine, manual, opening, closing',
            `reference_id`        BIGINT UNSIGNED NULL COMMENT 'ID of source record',
            `reference_number`    VARCHAR(50)     NULL COMMENT 'Source document number',
            `description_ar`      VARCHAR(500)    NOT NULL,
            `description_en`      VARCHAR(500)    NULL,
            `total_debit`         DECIMAL(18,2)   NOT NULL DEFAULT 0.00,
            `total_credit`        DECIMAL(18,2)   NOT NULL DEFAULT 0.00,
            `currency`            VARCHAR(3)      NOT NULL DEFAULT 'EGP',
            `status`              ENUM('draft','posted','reversed') NOT NULL DEFAULT 'draft',
            `posted_at`           DATETIME        NULL,
            `posted_by`           BIGINT UNSIGNED NULL,
            `reversed_entry_id`   BIGINT UNSIGNED NULL COMMENT 'If this reverses another entry',
            `reversal_of_id`      BIGINT UNSIGNED NULL COMMENT 'If reversed, points to reversal entry',
            `is_auto_generated`   TINYINT(1)      NOT NULL DEFAULT 0,
            `source_module`       VARCHAR(50)     NULL COMMENT 'payments, sales, hr, subscriptions, fines, inventory',
            `branch_id`           BIGINT UNSIGNED NULL,
            `cost_center_id`      BIGINT UNSIGNED NULL,
            `period_closed`       TINYINT(1)      NOT NULL DEFAULT 0 COMMENT 'Locked after period closing',
            `attachments`         JSON            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,

            UNIQUE INDEX `idx_je_entry_number`     (`entry_number`),
            INDEX `idx_je_fiscal_year`              (`fiscal_year_id`),
            INDEX `idx_je_entry_date`               (`entry_date`),
            INDEX `idx_je_status`                   (`status`),
            INDEX `idx_je_reference`                (`reference_type`, `reference_id`),
            INDEX `idx_je_source_module`            (`source_module`),
            INDEX `idx_je_branch`                   (`branch_id`),
            INDEX `idx_je_cost_center`              (`cost_center_id`),
            CONSTRAINT `fk_je_fiscal_year`    FOREIGN KEY (`fiscal_year_id`)    REFERENCES `fiscal_years`(`id`),
            CONSTRAINT `fk_je_posted_by`      FOREIGN KEY (`posted_by`)         REFERENCES `employees`(`id`),
            CONSTRAINT `fk_je_branch`         FOREIGN KEY (`branch_id`)         REFERENCES `branches`(`id`),
            CONSTRAINT `fk_je_cost_center`    FOREIGN KEY (`cost_center_id`)    REFERENCES `cost_centers`(`id`),
            CONSTRAINT `fk_je_created_by`     FOREIGN KEY (`created_by`)        REFERENCES `employees`(`id`),
            CONSTRAINT `fk_je_updated_by`     FOREIGN KEY (`updated_by`)        REFERENCES `employees`(`id`)
        ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci
    ",
    'down' => "DROP TABLE IF EXISTS `journal_entries`",
];
