<?php
declare(strict_types=1);

return [
    'up' => "
        CREATE TABLE `sa_registrations` (
            `id` BIGINT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
            `registration_number` VARCHAR(30) NOT NULL,
            `player_id` BIGINT UNSIGNED NOT NULL,
            `player_type` VARCHAR(20) NOT NULL COMMENT 'member, non_member',
            `member_id` BIGINT UNSIGNED NULL,
            `national_id` VARCHAR(20) NULL,
            `status` VARCHAR(20) NOT NULL DEFAULT 'in_progress' COMMENT 'in_progress, pending_payment, completed, cancelled',
            `registration_fee` DECIMAL(10,2) NOT NULL DEFAULT 0.00,
            `card_fee` DECIMAL(10,2) NOT NULL DEFAULT 0.00,
            `form_fee` DECIMAL(10,2) NOT NULL DEFAULT 0.00,
            `total_fees` DECIMAL(10,2) NOT NULL DEFAULT 0.00,
            `group_id` BIGINT UNSIGNED NULL,
            `subscription_amount` DECIMAL(10,2) NULL,
            `photo_captured` TINYINT(1) NOT NULL DEFAULT 0,
            `form_printed` TINYINT(1) NOT NULL DEFAULT 0,
            `card_generated` TINYINT(1) NOT NULL DEFAULT 0,
            `payment_request_id` BIGINT UNSIGNED NULL,
            `payment_status` VARCHAR(20) NOT NULL DEFAULT 'unpaid' COMMENT 'unpaid, pending, paid',
            `completed_at` TIMESTAMP NULL,
            `cancelled_at` TIMESTAMP NULL,
            `cancel_reason` VARCHAR(500) NULL,
            `branch_id` BIGINT UNSIGNED NULL,
            `created_by` BIGINT UNSIGNED NULL,
            `created_at` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
            `updated_at` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
            UNIQUE KEY `uq_sa_reg_number` (`registration_number`),
            INDEX `idx_sa_reg_player` (`player_id`),
            INDEX `idx_sa_reg_status` (`status`),
            INDEX `idx_sa_reg_date` (`created_at`),
            INDEX `idx_sa_reg_payment` (`payment_status`),
            CONSTRAINT `fk_sa_reg_player` FOREIGN KEY (`player_id`) REFERENCES `sa_players`(`id`) ON DELETE RESTRICT
        ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

        CREATE TABLE `sa_player_cards` (
            `id` BIGINT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
            `card_number` VARCHAR(30) NOT NULL,
            `player_id` BIGINT UNSIGNED NOT NULL,
            `registration_id` BIGINT UNSIGNED NULL,
            `card_type` VARCHAR(20) NOT NULL DEFAULT 'standard' COMMENT 'standard, temporary',
            `status` VARCHAR(20) NOT NULL DEFAULT 'active' COMMENT 'active, temporary, expired, suspended, revoked',
            `qr_code_data` VARCHAR(200) NULL,
            `valid_from` DATE NOT NULL,
            `valid_until` DATE NULL COMMENT 'NULL = no expiry; temporary = +7 days',
            `photo_path` VARCHAR(500) NULL,
            `issued_at` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
            `issued_by` BIGINT UNSIGNED NULL,
            `suspended_at` TIMESTAMP NULL,
            `suspended_by` BIGINT UNSIGNED NULL,
            `suspend_reason` VARCHAR(500) NULL,
            `revoked_at` TIMESTAMP NULL,
            `revoked_by` BIGINT UNSIGNED NULL,
            `revoke_reason` VARCHAR(500) NULL,
            `print_count` INT NOT NULL DEFAULT 0,
            `last_printed_at` TIMESTAMP NULL,
            `branch_id` BIGINT UNSIGNED NULL,
            `is_archived` TINYINT(1) NOT NULL DEFAULT 0,
            `created_at` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
            `updated_at` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
            UNIQUE KEY `uq_sa_card_number` (`card_number`),
            INDEX `idx_sa_card_player` (`player_id`),
            INDEX `idx_sa_card_status` (`status`),
            INDEX `idx_sa_card_valid` (`valid_until`),
            INDEX `idx_sa_card_registration` (`registration_id`),
            CONSTRAINT `fk_sa_card_player` FOREIGN KEY (`player_id`) REFERENCES `sa_players`(`id`) ON DELETE RESTRICT
        ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

        CREATE TABLE `sa_gate_access_log` (
            `id` BIGINT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
            `player_id` BIGINT UNSIGNED NOT NULL,
            `card_id` BIGINT UNSIGNED NULL,
            `access_type` VARCHAR(10) NOT NULL COMMENT 'entry, exit',
            `access_point` VARCHAR(100) NULL,
            `granted` TINYINT(1) NOT NULL DEFAULT 1,
            `denial_reason` VARCHAR(200) NULL,
            `scanned_data` VARCHAR(200) NULL,
            `recorded_by` BIGINT UNSIGNED NULL,
            `recorded_at` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
            INDEX `idx_sa_gate_player` (`player_id`),
            INDEX `idx_sa_gate_card` (`card_id`),
            INDEX `idx_sa_gate_date` (`recorded_at`),
            INDEX `idx_sa_gate_granted` (`granted`),
            CONSTRAINT `fk_sa_gate_player` FOREIGN KEY (`player_id`) REFERENCES `sa_players`(`id`) ON DELETE RESTRICT
        ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci
    ",
    'down' => "
        DROP TABLE IF EXISTS `sa_gate_access_log`;
        DROP TABLE IF EXISTS `sa_player_cards`;
        DROP TABLE IF EXISTS `sa_registrations`
    ",
];
