<?php
declare(strict_types=1);

use App\Core\Database;

return function (Database $db): void {
    $ts = date('Y-m-d H:i:s');

    // ── Reusable config building helpers ──

    $fullAgeGroups = [
        ['code' => 'u6',     'label_ar' => 'تحت 6',   'min_age' => 4,  'max_age' => 5],
        ['code' => 'u8',     'label_ar' => 'تحت 8',   'min_age' => 6,  'max_age' => 7],
        ['code' => 'u10',    'label_ar' => 'تحت 10',  'min_age' => 8,  'max_age' => 9],
        ['code' => 'u12',    'label_ar' => 'تحت 12',  'min_age' => 10, 'max_age' => 11],
        ['code' => 'u14',    'label_ar' => 'تحت 14',  'min_age' => 12, 'max_age' => 13],
        ['code' => 'u16',    'label_ar' => 'تحت 16',  'min_age' => 14, 'max_age' => 15],
        ['code' => 'u18',    'label_ar' => 'تحت 18',  'min_age' => 16, 'max_age' => 17],
        ['code' => 'senior', 'label_ar' => 'كبار',    'min_age' => 18, 'max_age' => 99],
    ];

    $compactAgeGroups = [
        ['code' => 'u10',    'label_ar' => 'تحت 10',  'min_age' => 6,  'max_age' => 9],
        ['code' => 'u14',    'label_ar' => 'تحت 14',  'min_age' => 10, 'max_age' => 13],
        ['code' => 'u18',    'label_ar' => 'تحت 18',  'min_age' => 14, 'max_age' => 17],
        ['code' => 'senior', 'label_ar' => 'كبار',    'min_age' => 18, 'max_age' => 99],
    ];

    $openAgeGroup = [
        ['code' => 'open', 'label_ar' => 'مفتوح', 'min_age' => 0, 'max_age' => 99],
    ];

    $fullSkillLevels = [
        ['code' => 'beginner',     'label_ar' => 'مبتدئ'],
        ['code' => 'intermediate', 'label_ar' => 'متوسط'],
        ['code' => 'advanced',     'label_ar' => 'متقدم'],
        ['code' => 'elite',        'label_ar' => 'نخبة'],
    ];

    $basicSkillLevels = [
        ['code' => 'beginner',     'label_ar' => 'مبتدئ'],
        ['code' => 'intermediate', 'label_ar' => 'متوسط'],
        ['code' => 'advanced',     'label_ar' => 'متقدم'],
    ];

    $leisureSkillLevels = [
        ['code' => 'casual', 'label_ar' => 'ترفيهي'],
    ];

    // ── Sport definitions ──

    $sports = [
        // ── individual ──
        [
            'code' => 'karate', 'name_ar' => 'كاراتيه', 'name_en' => 'Karate',
            'category' => 'individual', 'icon' => 'swords', 'sort_order' => 1,
            'config_json' => json_encode([
                'age_groups' => $fullAgeGroups,
                'skill_levels' => $fullSkillLevels,
                'requires_medical_cert' => true,
                'requires_guardian_consent_under' => 18,
                'max_players_per_session' => 20,
            ], JSON_UNESCAPED_UNICODE),
        ],
        [
            'code' => 'kung_fu', 'name_ar' => 'كونغ فو', 'name_en' => 'Kung Fu',
            'category' => 'individual', 'icon' => 'hand-metal', 'sort_order' => 2,
            'config_json' => json_encode([
                'age_groups' => $fullAgeGroups,
                'skill_levels' => $fullSkillLevels,
                'requires_medical_cert' => true,
                'requires_guardian_consent_under' => 18,
                'max_players_per_session' => 20,
            ], JSON_UNESCAPED_UNICODE),
        ],
        [
            'code' => 'gymnastics', 'name_ar' => 'جمباز', 'name_en' => 'Gymnastics',
            'category' => 'individual', 'icon' => 'accessibility', 'sort_order' => 3,
            'config_json' => json_encode([
                'age_groups' => $fullAgeGroups,
                'skill_levels' => $fullSkillLevels,
                'requires_medical_cert' => true,
                'requires_guardian_consent_under' => 18,
                'max_players_per_session' => 15,
            ], JSON_UNESCAPED_UNICODE),
        ],
        [
            'code' => 'swimming', 'name_ar' => 'سباحة', 'name_en' => 'Swimming',
            'category' => 'individual', 'icon' => 'waves', 'sort_order' => 4,
            'config_json' => json_encode([
                'age_groups' => $fullAgeGroups,
                'skill_levels' => $fullSkillLevels,
                'requires_medical_cert' => true,
                'requires_guardian_consent_under' => 18,
                'max_players_per_session' => 25,
            ], JSON_UNESCAPED_UNICODE),
        ],
        [
            'code' => 'archery', 'name_ar' => 'رماية بالقوس', 'name_en' => 'Archery',
            'category' => 'individual', 'icon' => 'target', 'sort_order' => 5,
            'config_json' => json_encode([
                'age_groups' => $compactAgeGroups,
                'skill_levels' => $fullSkillLevels,
                'requires_medical_cert' => true,
                'requires_guardian_consent_under' => 18,
                'max_players_per_session' => 12,
            ], JSON_UNESCAPED_UNICODE),
        ],
        [
            'code' => 'skating', 'name_ar' => 'تزلج', 'name_en' => 'Skating',
            'category' => 'individual', 'icon' => 'bike', 'sort_order' => 6,
            'config_json' => json_encode([
                'age_groups' => $compactAgeGroups,
                'skill_levels' => $basicSkillLevels,
                'requires_medical_cert' => true,
                'requires_guardian_consent_under' => 18,
                'max_players_per_session' => 15,
            ], JSON_UNESCAPED_UNICODE),
        ],
        [
            'code' => 'chess', 'name_ar' => 'شطرنج', 'name_en' => 'Chess',
            'category' => 'individual', 'icon' => 'crown', 'sort_order' => 7,
            'config_json' => json_encode([
                'age_groups' => $compactAgeGroups,
                'skill_levels' => $fullSkillLevels,
                'requires_medical_cert' => false,
                'requires_guardian_consent_under' => 18,
                'max_players_per_session' => 30,
            ], JSON_UNESCAPED_UNICODE),
        ],
        [
            'code' => 'darts', 'name_ar' => 'سهام', 'name_en' => 'Darts',
            'category' => 'individual', 'icon' => 'crosshair', 'sort_order' => 8,
            'config_json' => json_encode([
                'age_groups' => $compactAgeGroups,
                'skill_levels' => $basicSkillLevels,
                'requires_medical_cert' => false,
                'requires_guardian_consent_under' => 18,
                'max_players_per_session' => 16,
            ], JSON_UNESCAPED_UNICODE),
        ],

        // ── team ──
        [
            'code' => 'football', 'name_ar' => 'كرة قدم', 'name_en' => 'Football',
            'category' => 'team', 'icon' => 'trophy', 'sort_order' => 10,
            'config_json' => json_encode([
                'age_groups' => $fullAgeGroups,
                'skill_levels' => $fullSkillLevels,
                'requires_medical_cert' => true,
                'requires_guardian_consent_under' => 18,
                'max_players_per_session' => 22,
            ], JSON_UNESCAPED_UNICODE),
        ],
        [
            'code' => 'futsal', 'name_ar' => 'كرة صالات', 'name_en' => 'Futsal',
            'category' => 'team', 'icon' => 'goal', 'sort_order' => 11,
            'config_json' => json_encode([
                'age_groups' => $fullAgeGroups,
                'skill_levels' => $fullSkillLevels,
                'requires_medical_cert' => true,
                'requires_guardian_consent_under' => 18,
                'max_players_per_session' => 10,
            ], JSON_UNESCAPED_UNICODE),
        ],
        [
            'code' => 'basketball', 'name_ar' => 'كرة سلة', 'name_en' => 'Basketball',
            'category' => 'team', 'icon' => 'circle-dot', 'sort_order' => 12,
            'config_json' => json_encode([
                'age_groups' => $fullAgeGroups,
                'skill_levels' => $fullSkillLevels,
                'requires_medical_cert' => true,
                'requires_guardian_consent_under' => 18,
                'max_players_per_session' => 12,
            ], JSON_UNESCAPED_UNICODE),
        ],
        [
            'code' => 'handball', 'name_ar' => 'كرة يد', 'name_en' => 'Handball',
            'category' => 'team', 'icon' => 'hand', 'sort_order' => 13,
            'config_json' => json_encode([
                'age_groups' => $fullAgeGroups,
                'skill_levels' => $fullSkillLevels,
                'requires_medical_cert' => true,
                'requires_guardian_consent_under' => 18,
                'max_players_per_session' => 14,
            ], JSON_UNESCAPED_UNICODE),
        ],
        [
            'code' => 'volleyball', 'name_ar' => 'كرة طائرة', 'name_en' => 'Volleyball',
            'category' => 'team', 'icon' => 'globe', 'sort_order' => 14,
            'config_json' => json_encode([
                'age_groups' => $fullAgeGroups,
                'skill_levels' => $fullSkillLevels,
                'requires_medical_cert' => true,
                'requires_guardian_consent_under' => 18,
                'max_players_per_session' => 12,
            ], JSON_UNESCAPED_UNICODE),
        ],

        // ── racket ──
        [
            'code' => 'squash', 'name_ar' => 'اسكواش', 'name_en' => 'Squash',
            'category' => 'racket', 'icon' => 'zap', 'sort_order' => 20,
            'config_json' => json_encode([
                'age_groups' => $compactAgeGroups,
                'skill_levels' => $fullSkillLevels,
                'requires_medical_cert' => true,
                'requires_guardian_consent_under' => 18,
                'max_players_per_session' => 2,
            ], JSON_UNESCAPED_UNICODE),
        ],
        [
            'code' => 'table_tennis', 'name_ar' => 'تنس طاولة', 'name_en' => 'Table Tennis',
            'category' => 'racket', 'icon' => 'table', 'sort_order' => 21,
            'config_json' => json_encode([
                'age_groups' => $compactAgeGroups,
                'skill_levels' => $fullSkillLevels,
                'requires_medical_cert' => true,
                'requires_guardian_consent_under' => 18,
                'max_players_per_session' => 4,
            ], JSON_UNESCAPED_UNICODE),
        ],
        [
            'code' => 'tennis', 'name_ar' => 'تنس', 'name_en' => 'Tennis',
            'category' => 'racket', 'icon' => 'disc', 'sort_order' => 22,
            'config_json' => json_encode([
                'age_groups' => $compactAgeGroups,
                'skill_levels' => $fullSkillLevels,
                'requires_medical_cert' => true,
                'requires_guardian_consent_under' => 18,
                'max_players_per_session' => 4,
            ], JSON_UNESCAPED_UNICODE),
        ],
        [
            'code' => 'padel', 'name_ar' => 'بادل', 'name_en' => 'Padel',
            'category' => 'racket', 'icon' => 'rectangle-horizontal', 'sort_order' => 23,
            'config_json' => json_encode([
                'age_groups' => $compactAgeGroups,
                'skill_levels' => $fullSkillLevels,
                'requires_medical_cert' => true,
                'requires_guardian_consent_under' => 18,
                'max_players_per_session' => 4,
            ], JSON_UNESCAPED_UNICODE),
        ],

        // ── leisure ──
        [
            'code' => 'bowling', 'name_ar' => 'بولينج', 'name_en' => 'Bowling',
            'category' => 'leisure', 'icon' => 'circle', 'sort_order' => 30,
            'config_json' => json_encode([
                'age_groups' => $openAgeGroup,
                'skill_levels' => $leisureSkillLevels,
                'requires_medical_cert' => false,
                'requires_guardian_consent_under' => 12,
                'max_players_per_session' => 6,
            ], JSON_UNESCAPED_UNICODE),
        ],
        [
            'code' => 'billiards', 'name_ar' => 'بلياردو', 'name_en' => 'Billiards',
            'category' => 'leisure', 'icon' => 'hexagon', 'sort_order' => 31,
            'config_json' => json_encode([
                'age_groups' => $openAgeGroup,
                'skill_levels' => $leisureSkillLevels,
                'requires_medical_cert' => false,
                'requires_guardian_consent_under' => 12,
                'max_players_per_session' => 4,
            ], JSON_UNESCAPED_UNICODE),
        ],
        [
            'code' => 'playstation', 'name_ar' => 'بلايستيشن', 'name_en' => 'PlayStation',
            'category' => 'leisure', 'icon' => 'gamepad-2', 'sort_order' => 32,
            'config_json' => json_encode([
                'age_groups' => $openAgeGroup,
                'skill_levels' => $leisureSkillLevels,
                'requires_medical_cert' => false,
                'requires_guardian_consent_under' => 12,
                'max_players_per_session' => 2,
            ], JSON_UNESCAPED_UNICODE),
        ],

        // ── emerging ──
        [
            'code' => 'korffball', 'name_ar' => 'كرة القرف', 'name_en' => 'Korfball',
            'category' => 'emerging', 'icon' => 'rocket', 'sort_order' => 40,
            'config_json' => json_encode([
                'age_groups' => $compactAgeGroups,
                'skill_levels' => $basicSkillLevels,
                'requires_medical_cert' => true,
                'requires_guardian_consent_under' => 18,
                'max_players_per_session' => 16,
            ], JSON_UNESCAPED_UNICODE),
        ],
        [
            'code' => 'badball', 'name_ar' => 'باد بول', 'name_en' => 'Badball',
            'category' => 'emerging', 'icon' => 'flame', 'sort_order' => 41,
            'config_json' => json_encode([
                'age_groups' => $compactAgeGroups,
                'skill_levels' => $basicSkillLevels,
                'requires_medical_cert' => true,
                'requires_guardian_consent_under' => 18,
                'max_players_per_session' => 12,
            ], JSON_UNESCAPED_UNICODE),
        ],
    ];

    foreach ($sports as $sport) {
        $existing = $db->selectOne("SELECT id FROM sport_disciplines WHERE code = ?", [$sport['code']]);
        if ($existing) {
            continue;
        }
        $db->insert('sport_disciplines', [
            'code'           => $sport['code'],
            'name_ar'        => $sport['name_ar'],
            'name_en'        => $sport['name_en'],
            'category'       => $sport['category'],
            'icon'           => $sport['icon'],
            'config_json'    => $sport['config_json'],
            'sort_order'     => $sport['sort_order'],
            'is_active'      => 1,
            'created_at'     => $ts,
            'updated_at'     => $ts,
        ]);
    }
};
