<?php
declare(strict_types=1);

use App\Core\Database;

return function (Database $db): void {
    $db->raw("
        CREATE TABLE live_matches (
            id BIGINT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
            facility_id BIGINT UNSIGNED NULL,
            discipline_id BIGINT UNSIGNED NULL,
            tournament_match_id BIGINT UNSIGNED NULL,
            match_title_ar VARCHAR(200) NOT NULL,
            team_a_name VARCHAR(100) NOT NULL,
            team_b_name VARCHAR(100) NOT NULL,
            team_a_logo_path VARCHAR(500) NULL,
            team_b_logo_path VARCHAR(500) NULL,
            score_a INT NOT NULL DEFAULT 0,
            score_b INT NOT NULL DEFAULT 0,
            match_date DATE NOT NULL,
            start_time TIME NOT NULL,
            status ENUM('scheduled','live','finished','cancelled') NOT NULL DEFAULT 'scheduled',
            started_at DATETIME NULL,
            finished_at DATETIME NULL,
            referee_name VARCHAR(100) NULL,
            entry_employee_id BIGINT UNSIGNED NULL,
            notes TEXT NULL,
            created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
            updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
            INDEX idx_live_match_status (status),
            INDEX idx_live_match_date (match_date)
        ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci
    ");

    $db->raw("
        CREATE TABLE match_events (
            id BIGINT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
            match_id BIGINT UNSIGNED NOT NULL,
            minute INT UNSIGNED NOT NULL DEFAULT 0,
            event_type ENUM('goal','foul','yellow_card','red_card','substitution','penalty','injury','half_time','full_time','other') NOT NULL,
            team ENUM('a','b') NULL,
            player_name VARCHAR(100) NULL,
            description_ar TEXT NULL,
            created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
            INDEX idx_match_event_match (match_id),
            FOREIGN KEY (match_id) REFERENCES live_matches(id) ON DELETE CASCADE
        ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci
    ");
};
