<?php
declare(strict_types=1);

use App\Core\Database;

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

    // ── Helper: look up discipline ID by code ──
    $disciplineId = function (string $code) use ($db): int {
        $row = $db->selectOne("SELECT id FROM sport_disciplines WHERE code = ?", [$code]);
        if (!$row) {
            throw new \RuntimeException("Sport discipline '{$code}' not found. Run Phase 17 seeds first.");
        }
        return (int) $row['id'];
    };

    // ── Helper: insert academy idempotently, return its ID ──
    $ensureAcademy = function (array $data) use ($db, $ts): int {
        $existing = $db->selectOne("SELECT id FROM academies WHERE code = ?", [$data['code']]);
        if ($existing) {
            return (int) $existing['id'];
        }
        $db->insert('academies', array_merge($data, [
            'is_active'   => 1,
            'created_at'  => $ts,
            'updated_at'  => $ts,
        ]));
        $inserted = $db->selectOne("SELECT id FROM academies WHERE code = ?", [$data['code']]);
        return (int) $inserted['id'];
    };

    // ── Helper: insert level idempotently ──
    $ensureLevel = function (int $academyId, array $data) use ($db, $ts): void {
        $existing = $db->selectOne(
            "SELECT id FROM academy_levels WHERE academy_id = ? AND code = ?",
            [$academyId, $data['code']]
        );
        if ($existing) {
            return;
        }
        $db->insert('academy_levels', array_merge($data, [
            'academy_id'  => $academyId,
            'is_active'   => 1,
            'created_at'  => $ts,
            'updated_at'  => $ts,
        ]));
    };

    // ══════════════════════════════════════════════════════════════
    // Football Academies (discipline: football)
    // ══════════════════════════════════════════════════════════════
    $footballId = $disciplineId('football');

    // 1. Anderlecht Academy
    $acadId = $ensureAcademy([
        'code'          => 'acad_anderlecht',
        'name_ar'       => 'أكاديمية أندرلخت',
        'name_en'       => 'Anderlecht Academy',
        'discipline_id' => $footballId,
        'academy_type'  => 'football',
        'sort_order'    => 1,
    ]);
    $ensureLevel($acadId, ['code' => 'u6',  'name_ar' => 'تحت 6',  'name_en' => 'Under 6',  'level_order' => 1, 'age_min' => 4,  'age_max' => 5]);
    $ensureLevel($acadId, ['code' => 'u8',  'name_ar' => 'تحت 8',  'name_en' => 'Under 8',  'level_order' => 2, 'age_min' => 6,  'age_max' => 7]);
    $ensureLevel($acadId, ['code' => 'u10', 'name_ar' => 'تحت 10', 'name_en' => 'Under 10', 'level_order' => 3, 'age_min' => 8,  'age_max' => 9]);
    $ensureLevel($acadId, ['code' => 'u12', 'name_ar' => 'تحت 12', 'name_en' => 'Under 12', 'level_order' => 4, 'age_min' => 10, 'age_max' => 11]);
    $ensureLevel($acadId, ['code' => 'u14', 'name_ar' => 'تحت 14', 'name_en' => 'Under 14', 'level_order' => 5, 'age_min' => 12, 'age_max' => 13]);
    $ensureLevel($acadId, ['code' => 'u16', 'name_ar' => 'تحت 16', 'name_en' => 'Under 16', 'level_order' => 6, 'age_min' => 14, 'age_max' => 15]);
    $ensureLevel($acadId, ['code' => 'u18', 'name_ar' => 'تحت 18', 'name_en' => 'Under 18', 'level_order' => 7, 'age_min' => 16, 'age_max' => 17]);

    // 2. Mowahiba Academy
    $acadId = $ensureAcademy([
        'code'          => 'acad_mowahiba',
        'name_ar'       => 'أكاديمية مواهب',
        'name_en'       => 'Mowahiba Academy',
        'discipline_id' => $footballId,
        'academy_type'  => 'football',
        'sort_order'    => 2,
    ]);
    $ensureLevel($acadId, ['code' => 'u8',  'name_ar' => 'تحت 8',  'name_en' => 'Under 8',  'level_order' => 1, 'age_min' => 6,  'age_max' => 7]);
    $ensureLevel($acadId, ['code' => 'u10', 'name_ar' => 'تحت 10', 'name_en' => 'Under 10', 'level_order' => 2, 'age_min' => 8,  'age_max' => 9]);
    $ensureLevel($acadId, ['code' => 'u12', 'name_ar' => 'تحت 12', 'name_en' => 'Under 12', 'level_order' => 3, 'age_min' => 10, 'age_max' => 11]);
    $ensureLevel($acadId, ['code' => 'u14', 'name_ar' => 'تحت 14', 'name_en' => 'Under 14', 'level_order' => 4, 'age_min' => 12, 'age_max' => 13]);

    // 3. Al-Rowad Academy
    $acadId = $ensureAcademy([
        'code'          => 'acad_rowad',
        'name_ar'       => 'أكاديمية الرواد',
        'name_en'       => 'Al-Rowad Academy',
        'discipline_id' => $footballId,
        'academy_type'  => 'football',
        'sort_order'    => 3,
    ]);
    $ensureLevel($acadId, ['code' => 'u10', 'name_ar' => 'تحت 10', 'name_en' => 'Under 10', 'level_order' => 1, 'age_min' => 6,  'age_max' => 9]);
    $ensureLevel($acadId, ['code' => 'u14', 'name_ar' => 'تحت 14', 'name_en' => 'Under 14', 'level_order' => 2, 'age_min' => 10, 'age_max' => 13]);
    $ensureLevel($acadId, ['code' => 'u18', 'name_ar' => 'تحت 18', 'name_en' => 'Under 18', 'level_order' => 3, 'age_min' => 14, 'age_max' => 17]);

    // ══════════════════════════════════════════════════════════════
    // Gymnastics Academies (discipline: gymnastics)
    // ══════════════════════════════════════════════════════════════
    $gymnasticsId = $disciplineId('gymnastics');

    // 4. Artistic Gymnastics - Men
    $acadId = $ensureAcademy([
        'code'          => 'acad_gym_artistic_m',
        'name_ar'       => 'جمباز فني - رجال',
        'name_en'       => 'Artistic Gymnastics - Men',
        'discipline_id' => $gymnasticsId,
        'academy_type'  => 'gymnastics',
        'sort_order'    => 10,
    ]);
    $ensureLevel($acadId, ['code' => 'beginners',    'name_ar' => 'مبتدئين', 'name_en' => 'Beginners',    'level_order' => 1, 'age_min' => 4,  'age_max' => 7]);
    $ensureLevel($acadId, ['code' => 'intermediate', 'name_ar' => 'متوسط',   'name_en' => 'Intermediate', 'level_order' => 2, 'age_min' => 8,  'age_max' => 11]);
    $ensureLevel($acadId, ['code' => 'advanced',     'name_ar' => 'متقدم',   'name_en' => 'Advanced',     'level_order' => 3, 'age_min' => 12, 'age_max' => 17]);

    // 5. Artistic Gymnastics - Women
    $acadId = $ensureAcademy([
        'code'          => 'acad_gym_artistic_w',
        'name_ar'       => 'جمباز فني - سيدات',
        'name_en'       => 'Artistic Gymnastics - Women',
        'discipline_id' => $gymnasticsId,
        'academy_type'  => 'gymnastics',
        'sort_order'    => 11,
    ]);
    $ensureLevel($acadId, ['code' => 'beginners',    'name_ar' => 'مبتدئين', 'name_en' => 'Beginners',    'level_order' => 1, 'age_min' => 4,  'age_max' => 7]);
    $ensureLevel($acadId, ['code' => 'intermediate', 'name_ar' => 'متوسط',   'name_en' => 'Intermediate', 'level_order' => 2, 'age_min' => 8,  'age_max' => 11]);
    $ensureLevel($acadId, ['code' => 'advanced',     'name_ar' => 'متقدم',   'name_en' => 'Advanced',     'level_order' => 3, 'age_min' => 12, 'age_max' => 17]);

    // 6. Aerobic Gymnastics
    $acadId = $ensureAcademy([
        'code'          => 'acad_gym_aerobic',
        'name_ar'       => 'جمباز إيروبك',
        'name_en'       => 'Aerobic Gymnastics',
        'discipline_id' => $gymnasticsId,
        'academy_type'  => 'gymnastics',
        'sort_order'    => 12,
    ]);
    $ensureLevel($acadId, ['code' => 'general', 'name_ar' => 'عام', 'name_en' => 'General', 'level_order' => 1, 'age_min' => 6, 'age_max' => 99]);

    // ══════════════════════════════════════════════════════════════
    // Swimming Academies (discipline: swimming)
    // ══════════════════════════════════════════════════════════════
    $swimmingId = $disciplineId('swimming');

    // 7. Swimming Schools
    $acadId = $ensureAcademy([
        'code'          => 'acad_swim_schools',
        'name_ar'       => 'مدارس السباحة',
        'name_en'       => 'Swimming Schools',
        'discipline_id' => $swimmingId,
        'academy_type'  => 'swimming',
        'sort_order'    => 20,
    ]);
    $ensureLevel($acadId, ['code' => 'level_1', 'name_ar' => 'المستوى الأول',  'name_en' => 'Level 1', 'level_order' => 1, 'age_min' => 4,  'age_max' => 6]);
    $ensureLevel($acadId, ['code' => 'level_2', 'name_ar' => 'المستوى الثاني', 'name_en' => 'Level 2', 'level_order' => 2, 'age_min' => 6,  'age_max' => 8]);
    $ensureLevel($acadId, ['code' => 'level_3', 'name_ar' => 'المستوى الثالث', 'name_en' => 'Level 3', 'level_order' => 3, 'age_min' => 8,  'age_max' => 12]);

    // 8. Swimming Practice
    $acadId = $ensureAcademy([
        'code'          => 'acad_swim_practice',
        'name_ar'       => 'تدريب السباحة',
        'name_en'       => 'Swimming Practice',
        'discipline_id' => $swimmingId,
        'academy_type'  => 'swimming',
        'sort_order'    => 21,
    ]);
    $ensureLevel($acadId, ['code' => 'junior',       'name_ar' => 'ناشئين', 'name_en' => 'Junior',       'level_order' => 1, 'age_min' => 8,  'age_max' => 12]);
    $ensureLevel($acadId, ['code' => 'intermediate', 'name_ar' => 'متوسط',  'name_en' => 'Intermediate', 'level_order' => 2, 'age_min' => 12, 'age_max' => 15]);
    $ensureLevel($acadId, ['code' => 'senior',       'name_ar' => 'كبار',   'name_en' => 'Senior',       'level_order' => 3, 'age_min' => 15, 'age_max' => 99]);

    // 9. Advanced Swimming
    $acadId = $ensureAcademy([
        'code'          => 'acad_swim_advanced',
        'name_ar'       => 'السباحة المتقدمة',
        'name_en'       => 'Advanced Swimming',
        'discipline_id' => $swimmingId,
        'academy_type'  => 'swimming',
        'sort_order'    => 22,
    ]);
    $ensureLevel($acadId, ['code' => 'pre_national', 'name_ar' => 'ما قبل المنتخب', 'name_en' => 'Pre-National', 'level_order' => 1, 'age_min' => 12, 'age_max' => 17]);
    $ensureLevel($acadId, ['code' => 'national',     'name_ar' => 'منتخب',           'name_en' => 'National',     'level_order' => 2, 'age_min' => 14, 'age_max' => 99]);
};
