<?php
declare(strict_types=1);

return [
    'up' => "
        CREATE TABLE IF NOT EXISTS `achievement_definitions` (
            `id` BIGINT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
            `code` VARCHAR(50) NOT NULL,
            `name_ar` VARCHAR(200) NOT NULL,
            `name_en` VARCHAR(200) NULL,
            `description_ar` TEXT NULL,
            `category` VARCHAR(30) NOT NULL COMMENT 'attendance, performance, milestone, competition, social',
            `icon` VARCHAR(50) NULL,
            `badge_color` VARCHAR(20) NULL DEFAULT '#FFD700',
            `discipline_id` BIGINT UNSIGNED NULL COMMENT 'NULL = applies to all sports',
            `criteria_json` JSON NOT NULL COMMENT 'auto-award rules: {type, threshold, period}',
            `points` INT UNSIGNED NOT NULL DEFAULT 10,
            `is_active` TINYINT(1) NOT NULL DEFAULT 1,
            `created_at` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
            UNIQUE KEY `uq_ad_code` (`code`),
            INDEX `idx_ad_category` (`category`),
            INDEX `idx_ad_discipline` (`discipline_id`)
        ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

        CREATE TABLE IF NOT EXISTS `player_achievements` (
            `id` BIGINT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
            `player_id` BIGINT UNSIGNED NOT NULL,
            `achievement_id` BIGINT UNSIGNED NOT NULL,
            `earned_at` DATETIME NOT NULL,
            `context_json` JSON NULL COMMENT 'what triggered it: session_id, competition_id, etc.',
            `points_earned` INT UNSIGNED NOT NULL DEFAULT 0,
            `notified` TINYINT(1) NOT NULL DEFAULT 0,
            `created_at` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
            INDEX `idx_pa_player` (`player_id`),
            INDEX `idx_pa_achievement` (`achievement_id`),
            INDEX `idx_pa_date` (`earned_at`),
            CONSTRAINT `fk_pach_player` FOREIGN KEY (`player_id`) REFERENCES `players`(`id`),
            CONSTRAINT `fk_pach_achievement` FOREIGN KEY (`achievement_id`) REFERENCES `achievement_definitions`(`id`)
        ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

        CREATE TABLE IF NOT EXISTS `player_progression_log` (
            `id` BIGINT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
            `player_id` BIGINT UNSIGNED NOT NULL,
            `discipline_id` BIGINT UNSIGNED NOT NULL,
            `event_type` VARCHAR(30) NOT NULL COMMENT 'level_up, group_change, evaluation, award, milestone',
            `from_value` VARCHAR(100) NULL,
            `to_value` VARCHAR(100) NULL,
            `description_ar` TEXT NULL,
            `related_entity_type` VARCHAR(50) NULL,
            `related_entity_id` BIGINT UNSIGNED NULL,
            `recorded_at` DATETIME NOT NULL,
            `recorded_by` BIGINT UNSIGNED NULL,
            `created_at` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
            INDEX `idx_ppl_player` (`player_id`),
            INDEX `idx_ppl_discipline` (`discipline_id`),
            INDEX `idx_ppl_date` (`recorded_at`),
            CONSTRAINT `fk_pplog_player` FOREIGN KEY (`player_id`) REFERENCES `players`(`id`)
        ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci
    ",
    'down' => "
        DROP TABLE IF EXISTS `player_progression_log`;
        DROP TABLE IF EXISTS `player_achievements`;
        DROP TABLE IF EXISTS `achievement_definitions`
    ",
];
