<?php
declare(strict_types=1);

return [
    'up' => "
        CREATE TABLE IF NOT EXISTS `chart_of_accounts` (
            `id`              BIGINT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
            `account_code`    VARCHAR(20)     NOT NULL COMMENT 'Hierarchical code e.g. 1101, 110101',
            `name_ar`         VARCHAR(200)    NOT NULL,
            `name_en`         VARCHAR(200)    NOT NULL,
            `account_type`    ENUM('asset','liability','equity','revenue','expense') NOT NULL,
            `account_nature`  ENUM('debit','credit') NOT NULL,
            `parent_id`       BIGINT UNSIGNED NULL COMMENT 'Self-referencing for tree structure',
            `level`           TINYINT UNSIGNED NOT NULL DEFAULT 1 COMMENT '1=category, 2=group, 3=sub-group, 4=detail',
            `is_header`       TINYINT(1)      NOT NULL DEFAULT 0 COMMENT '1=parent only, cannot post to',
            `is_active`       TINYINT(1)      NOT NULL DEFAULT 1,
            `is_system`       TINYINT(1)      NOT NULL DEFAULT 0 COMMENT 'System accounts cannot be deleted',
            `description_ar`  TEXT            NULL,
            `description_en`  TEXT            NULL,
            `opening_balance` DECIMAL(18,2)   NOT NULL DEFAULT 0.00,
            `current_balance` DECIMAL(18,2)   NOT NULL DEFAULT 0.00,
            `currency`        VARCHAR(3)      NOT NULL DEFAULT 'EGP',
            `cost_center_id`  BIGINT UNSIGNED NULL COMMENT 'Default cost center',
            `is_bank_account` TINYINT(1)      NOT NULL DEFAULT 0,
            `bank_account_id` BIGINT UNSIGNED NULL COMMENT 'Links to bank_accounts table',
            `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_coa_account_code`   (`account_code`),
            INDEX `idx_coa_account_type`           (`account_type`),
            INDEX `idx_coa_parent_id`              (`parent_id`),
            INDEX `idx_coa_level`                  (`level`),
            INDEX `idx_coa_is_active`              (`is_active`),
            INDEX `idx_coa_is_header`              (`is_header`),
            CONSTRAINT `fk_coa_parent`       FOREIGN KEY (`parent_id`)  REFERENCES `chart_of_accounts`(`id`),
            CONSTRAINT `fk_coa_created_by`   FOREIGN KEY (`created_by`) REFERENCES `employees`(`id`),
            CONSTRAINT `fk_coa_updated_by`   FOREIGN KEY (`updated_by`) REFERENCES `employees`(`id`)
        ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci
    ",
    'down' => "DROP TABLE IF EXISTS `chart_of_accounts`",
];
