<?php
declare(strict_types=1);

return [
    'up' => "
        CREATE TABLE IF NOT EXISTS `scheduling_rules` (
            `id` BIGINT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
            `rule_name_ar` VARCHAR(200) NOT NULL,
            `rule_type` VARCHAR(30) NOT NULL COMMENT 'coach_max_hours, facility_buffer, age_restriction, gender_separation, rest_between_sessions',
            `applies_to` VARCHAR(30) NOT NULL DEFAULT 'all' COMMENT 'all, discipline, facility, coach',
            `applies_to_id` BIGINT UNSIGNED NULL,
            `config_json` JSON NOT NULL COMMENT 'rule parameters',
            `priority` INT UNSIGNED NOT NULL DEFAULT 5,
            `is_active` TINYINT(1) NOT NULL DEFAULT 1,
            `created_at` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
            `updated_at` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
            INDEX `idx_sr_type` (`rule_type`),
            INDEX `idx_sr_active` (`is_active`)
        ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

        CREATE TABLE IF NOT EXISTS `scheduling_conflicts` (
            `id` BIGINT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
            `conflict_type` VARCHAR(30) NOT NULL COMMENT 'coach_overlap, facility_overlap, player_overlap, rule_violation, capacity_exceeded',
            `severity` VARCHAR(10) NOT NULL DEFAULT 'warning' COMMENT 'warning, error, info',
            `entity_a_type` VARCHAR(30) NOT NULL,
            `entity_a_id` BIGINT UNSIGNED NOT NULL,
            `entity_b_type` VARCHAR(30) NULL,
            `entity_b_id` BIGINT UNSIGNED NULL,
            `conflict_date` DATE NOT NULL,
            `conflict_time_start` TIME NOT NULL,
            `conflict_time_end` TIME NOT NULL,
            `description_ar` TEXT NOT NULL,
            `resolution` VARCHAR(30) NULL COMMENT 'auto_resolved, manually_resolved, ignored, pending',
            `resolved_by` BIGINT UNSIGNED NULL,
            `resolved_at` DATETIME NULL,
            `resolution_notes` TEXT NULL,
            `detected_at` DATETIME NOT NULL,
            `created_at` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
            INDEX `idx_sc_type` (`conflict_type`),
            INDEX `idx_sc_date` (`conflict_date`),
            INDEX `idx_sc_resolution` (`resolution`),
            INDEX `idx_sc_severity` (`severity`)
        ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

        CREATE TABLE IF NOT EXISTS `schedule_change_requests` (
            `id` BIGINT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
            `requester_type` VARCHAR(20) NOT NULL COMMENT 'coach, player, parent, admin',
            `requester_id` BIGINT UNSIGNED NOT NULL,
            `request_type` VARCHAR(30) NOT NULL COMMENT 'reschedule, swap, cancel, add_extra',
            `current_session_id` BIGINT UNSIGNED NULL,
            `current_group_id` BIGINT UNSIGNED NULL,
            `requested_day_of_week` TINYINT UNSIGNED NULL,
            `requested_start_time` TIME NULL,
            `requested_end_time` TIME NULL,
            `requested_date` DATE NULL,
            `requested_facility_id` BIGINT UNSIGNED NULL,
            `reason_ar` TEXT NOT NULL,
            `status` VARCHAR(20) NOT NULL DEFAULT 'pending' COMMENT 'pending, approved, rejected, auto_resolved',
            `reviewed_by` BIGINT UNSIGNED NULL,
            `reviewed_at` DATETIME NULL,
            `review_notes` TEXT NULL,
            `auto_suggestions_json` JSON NULL COMMENT 'system-generated alternative slots',
            `created_at` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
            `updated_at` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
            INDEX `idx_scr_status` (`status`),
            INDEX `idx_scr_requester` (`requester_type`, `requester_id`),
            INDEX `idx_scr_type` (`request_type`)
        ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci
    ",
    'down' => "
        DROP TABLE IF EXISTS `schedule_change_requests`;
        DROP TABLE IF EXISTS `scheduling_conflicts`;
        DROP TABLE IF EXISTS `scheduling_rules`
    ",
];
