<?php
declare(strict_types=1);

return [
    'up' => "
        CREATE TABLE IF NOT EXISTS `accounts_receivable` (
            `id`                BIGINT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
            `member_id`         BIGINT UNSIGNED NOT NULL,
            `document_type`     VARCHAR(50)     NOT NULL COMMENT 'installment, subscription, fine, membership_fee, other',
            `document_id`       BIGINT UNSIGNED NULL COMMENT 'ID of source: installment_plans.id, subscriptions.id, fines.id',
            `document_number`   VARCHAR(50)     NULL,
            `document_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','written_off','cancelled') NOT NULL DEFAULT 'pending',
            `journal_entry_id`  BIGINT UNSIGNED NULL COMMENT 'Original receivable entry',
            `payment_entry_id`  BIGINT UNSIGNED NULL COMMENT 'Payment/settlement entry',
            `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_ar_member`         (`member_id`),
            INDEX `idx_ar_status`          (`status`),
            INDEX `idx_ar_due_date`        (`due_date`),
            INDEX `idx_ar_document`        (`document_type`, `document_id`),
            INDEX `idx_ar_cost_center`     (`cost_center_id`),
            INDEX `idx_ar_branch`          (`branch_id`),
            CONSTRAINT `fk_ar_member`        FOREIGN KEY (`member_id`)        REFERENCES `members`(`id`),
            CONSTRAINT `fk_ar_journal_entry` FOREIGN KEY (`journal_entry_id`) REFERENCES `journal_entries`(`id`),
            CONSTRAINT `fk_ar_payment_entry` FOREIGN KEY (`payment_entry_id`) REFERENCES `journal_entries`(`id`),
            CONSTRAINT `fk_ar_cost_center`   FOREIGN KEY (`cost_center_id`)   REFERENCES `cost_centers`(`id`),
            CONSTRAINT `fk_ar_branch`        FOREIGN KEY (`branch_id`)        REFERENCES `branches`(`id`),
            CONSTRAINT `fk_ar_created_by`    FOREIGN KEY (`created_by`)       REFERENCES `employees`(`id`)
        ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci
    ",
    'down' => "DROP TABLE IF EXISTS `accounts_receivable`",
];
