<?php
declare(strict_types=1);

return [
    'up' => "
        CREATE TABLE IF NOT EXISTS `tournaments` (
            `id` BIGINT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
            `name_ar` VARCHAR(300) NOT NULL,
            `name_en` VARCHAR(300) NULL,
            `discipline_id` BIGINT UNSIGNED NOT NULL,
            `tournament_type` VARCHAR(30) NOT NULL COMMENT 'internal, friendly, league, championship, knockout, round_robin',
            `format` VARCHAR(30) NOT NULL DEFAULT 'knockout' COMMENT 'knockout, round_robin, group_stage, swiss, ladder',
            `age_group` VARCHAR(50) NULL,
            `gender` VARCHAR(10) NULL DEFAULT 'mixed',
            `max_participants` INT UNSIGNED NULL,
            `min_participants` INT UNSIGNED NULL DEFAULT 2,
            `registration_start` DATE NULL,
            `registration_end` DATE NULL,
            `start_date` DATE NOT NULL,
            `end_date` DATE NULL,
            `facility_id` BIGINT UNSIGNED NULL,
            `entry_fee` DECIMAL(10,2) NOT NULL DEFAULT 0,
            `prize_description_ar` TEXT NULL,
            `rules_ar` TEXT NULL,
            `status` VARCHAR(20) NOT NULL DEFAULT 'draft' COMMENT 'draft, registration_open, registration_closed, in_progress, completed, cancelled',
            `winner_player_id` BIGINT UNSIGNED NULL,
            `winner_team_name` VARCHAR(200) NULL,
            `organizer_id` BIGINT UNSIGNED NULL,
            `banner_image` VARCHAR(500) NULL,
            `notes` TEXT NULL,
            `created_at` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
            `updated_at` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
            `created_by` BIGINT UNSIGNED NULL,
            INDEX `idx_t_discipline` (`discipline_id`),
            INDEX `idx_t_status` (`status`),
            INDEX `idx_t_dates` (`start_date`, `end_date`),
            INDEX `idx_t_type` (`tournament_type`)
        ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

        CREATE TABLE IF NOT EXISTS `tournament_participants` (
            `id` BIGINT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
            `tournament_id` BIGINT UNSIGNED NOT NULL,
            `player_id` BIGINT UNSIGNED NULL,
            `team_name` VARCHAR(200) NULL,
            `team_members_json` JSON NULL COMMENT '[{player_id, name, role}]',
            `seed_number` INT UNSIGNED NULL,
            `registration_date` DATETIME NOT NULL,
            `status` VARCHAR(20) NOT NULL DEFAULT 'registered' COMMENT 'registered, confirmed, withdrawn, disqualified, eliminated, winner, runner_up',
            `final_position` INT UNSIGNED NULL,
            `points` INT UNSIGNED NOT NULL DEFAULT 0,
            `wins` INT UNSIGNED NOT NULL DEFAULT 0,
            `losses` INT UNSIGNED NOT NULL DEFAULT 0,
            `draws` INT UNSIGNED NOT NULL DEFAULT 0,
            `payment_id` BIGINT UNSIGNED NULL,
            `notes` TEXT NULL,
            `created_at` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
            INDEX `idx_tp_tournament` (`tournament_id`),
            INDEX `idx_tp_player` (`player_id`),
            INDEX `idx_tp_status` (`status`),
            CONSTRAINT `fk_tp_tournament` FOREIGN KEY (`tournament_id`) REFERENCES `tournaments`(`id`) ON DELETE CASCADE
        ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

        CREATE TABLE IF NOT EXISTS `tournament_matches` (
            `id` BIGINT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
            `tournament_id` BIGINT UNSIGNED NOT NULL,
            `round_number` INT UNSIGNED NOT NULL DEFAULT 1,
            `match_number` INT UNSIGNED NOT NULL,
            `group_name` VARCHAR(10) NULL COMMENT 'A, B, C for group stages',
            `participant_a_id` BIGINT UNSIGNED NULL,
            `participant_b_id` BIGINT UNSIGNED NULL,
            `scheduled_date` DATE NULL,
            `scheduled_time` TIME NULL,
            `facility_id` BIGINT UNSIGNED NULL,
            `score_a` VARCHAR(50) NULL COMMENT 'flexible: 3-2, 6-4 6-3, etc.',
            `score_b` VARCHAR(50) NULL,
            `winner_participant_id` BIGINT UNSIGNED NULL,
            `is_draw` TINYINT(1) NOT NULL DEFAULT 0,
            `status` VARCHAR(20) NOT NULL DEFAULT 'scheduled' COMMENT 'scheduled, in_progress, completed, cancelled, walkover',
            `referee_name` VARCHAR(200) NULL,
            `match_notes` TEXT NULL,
            `completed_at` DATETIME NULL,
            `created_at` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
            `updated_at` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
            INDEX `idx_tm_tournament` (`tournament_id`),
            INDEX `idx_tm_round` (`round_number`),
            INDEX `idx_tm_date` (`scheduled_date`),
            INDEX `idx_tm_status` (`status`),
            CONSTRAINT `fk_tm_tournament` FOREIGN KEY (`tournament_id`) REFERENCES `tournaments`(`id`) ON DELETE CASCADE
        ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci
    ",
    'down' => "
        DROP TABLE IF EXISTS `tournament_matches`;
        DROP TABLE IF EXISTS `tournament_participants`;
        DROP TABLE IF EXISTS `tournaments`
    ",
];
