<?php
declare(strict_types=1);

return [
    'up' => "
        CREATE TABLE IF NOT EXISTS `journal_entry_lines` (
            `id`               BIGINT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
            `journal_entry_id` BIGINT UNSIGNED NOT NULL,
            `line_number`      SMALLINT UNSIGNED NOT NULL DEFAULT 1,
            `account_id`       BIGINT UNSIGNED NOT NULL COMMENT 'chart_of_accounts.id',
            `description_ar`   VARCHAR(300)    NULL,
            `description_en`   VARCHAR(300)    NULL,
            `debit`            DECIMAL(18,2)   NOT NULL DEFAULT 0.00,
            `credit`           DECIMAL(18,2)   NOT NULL DEFAULT 0.00,
            `currency`         VARCHAR(3)      NOT NULL DEFAULT 'EGP',
            `cost_center_id`   BIGINT UNSIGNED NULL COMMENT 'Line-level cost center override',
            `branch_id`        BIGINT UNSIGNED NULL COMMENT 'Line-level branch override',
            `member_id`        BIGINT UNSIGNED NULL COMMENT 'For member-related AR/AP tracking',
            `employee_id`      BIGINT UNSIGNED NULL COMMENT 'For employee-related entries',
            `supplier_id`      BIGINT UNSIGNED NULL COMMENT 'For supplier-related AP tracking',
            `reference_type`   VARCHAR(50)     NULL COMMENT 'Sub-reference for line detail',
            `reference_id`     BIGINT UNSIGNED NULL,
            `notes`            VARCHAR(300)    NULL,
            `created_at`       DATETIME        NOT NULL DEFAULT CURRENT_TIMESTAMP,

            INDEX `idx_jel_journal_entry`      (`journal_entry_id`),
            INDEX `idx_jel_account`             (`account_id`),
            INDEX `idx_jel_cost_center`         (`cost_center_id`),
            INDEX `idx_jel_branch`              (`branch_id`),
            INDEX `idx_jel_member`              (`member_id`),
            INDEX `idx_jel_employee`            (`employee_id`),
            INDEX `idx_jel_supplier`            (`supplier_id`),
            CONSTRAINT `fk_jel_journal_entry` FOREIGN KEY (`journal_entry_id`) REFERENCES `journal_entries`(`id`) ON DELETE CASCADE,
            CONSTRAINT `fk_jel_account`       FOREIGN KEY (`account_id`)       REFERENCES `chart_of_accounts`(`id`),
            CONSTRAINT `fk_jel_cost_center`   FOREIGN KEY (`cost_center_id`)   REFERENCES `cost_centers`(`id`),
            CONSTRAINT `fk_jel_branch`        FOREIGN KEY (`branch_id`)        REFERENCES `branches`(`id`),
            CONSTRAINT `fk_jel_member`        FOREIGN KEY (`member_id`)        REFERENCES `members`(`id`),
            CONSTRAINT `fk_jel_employee`      FOREIGN KEY (`employee_id`)      REFERENCES `employees`(`id`),
            CONSTRAINT `fk_jel_supplier`      FOREIGN KEY (`supplier_id`)      REFERENCES `suppliers`(`id`)
        ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci
    ",
    'down' => "DROP TABLE IF EXISTS `journal_entry_lines`",
];
