<?php
declare(strict_types=1);

return [
    'up' => "
        CREATE TABLE IF NOT EXISTS `period_closings` (
            `id`                BIGINT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
            `fiscal_year_id`    BIGINT UNSIGNED NOT NULL,
            `period`            VARCHAR(7)      NOT NULL COMMENT 'YYYY-MM',
            `period_start`      DATE            NOT NULL,
            `period_end`        DATE            NOT NULL,
            `closing_type`      ENUM('monthly','yearly') NOT NULL DEFAULT 'monthly',
            `status`            ENUM('open','closing','closed','reopened') NOT NULL DEFAULT 'open',
            `total_debit`       DECIMAL(18,2)   NOT NULL DEFAULT 0.00,
            `total_credit`      DECIMAL(18,2)   NOT NULL DEFAULT 0.00,
            `journal_count`     INT UNSIGNED    NOT NULL DEFAULT 0,
            `closing_entry_id`  BIGINT UNSIGNED NULL COMMENT 'Year-end closing journal entry',
            `closed_at`         DATETIME        NULL,
            `closed_by`         BIGINT UNSIGNED NULL,
            `reopened_at`       DATETIME        NULL,
            `reopened_by`       BIGINT UNSIGNED NULL,
            `reopen_reason`     TEXT            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_pc_unique_period` (`fiscal_year_id`, `period`),
            INDEX `idx_pc_status`                (`status`),
            INDEX `idx_pc_closing_type`           (`closing_type`),
            CONSTRAINT `fk_pc_fiscal_year`    FOREIGN KEY (`fiscal_year_id`)   REFERENCES `fiscal_years`(`id`),
            CONSTRAINT `fk_pc_closing_entry`  FOREIGN KEY (`closing_entry_id`) REFERENCES `journal_entries`(`id`),
            CONSTRAINT `fk_pc_closed_by`      FOREIGN KEY (`closed_by`)        REFERENCES `employees`(`id`),
            CONSTRAINT `fk_pc_reopened_by`    FOREIGN KEY (`reopened_by`)      REFERENCES `employees`(`id`),
            CONSTRAINT `fk_pc_created_by`     FOREIGN KEY (`created_by`)       REFERENCES `employees`(`id`)
        ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci
    ",
    'down' => "DROP TABLE IF EXISTS `period_closings`",
];
