<?php
declare(strict_types=1);

use App\Core\Database;

return function (Database $db): void {
    $now = date('Y-m-d H:i:s');
    $disciplineId = 1; // SWIMMING

    // Kids Academy (6 months to 12 years)
    $kidsPrograms = [
        [
            'code'         => 'SWIM-KIDS-G5',
            'name_ar'      => 'أكاديمية السباحة أطفال — جروب 5',
            'name_en'      => 'Swimming Academy Kids — Group 5',
            'age_from'     => 1,
            'age_to'       => 12,
            'duration'     => 55,
            'fee_member'   => 480,
            'fee_nonmember'=> 960,
        ],
        [
            'code'         => 'SWIM-KIDS-G4',
            'name_ar'      => 'أكاديمية السباحة أطفال — جروب 4',
            'name_en'      => 'Swimming Academy Kids — Group 4',
            'age_from'     => 1,
            'age_to'       => 12,
            'duration'     => 55,
            'fee_member'   => 600,
            'fee_nonmember'=> 1200,
        ],
        [
            'code'         => 'SWIM-KIDS-G3',
            'name_ar'      => 'أكاديمية السباحة أطفال — جروب 3',
            'name_en'      => 'Swimming Academy Kids — Group 3',
            'age_from'     => 1,
            'age_to'       => 12,
            'duration'     => 55,
            'fee_member'   => 720,
            'fee_nonmember'=> 1440,
        ],
        [
            'code'         => 'SWIM-KIDS-G2',
            'name_ar'      => 'أكاديمية السباحة أطفال — جروب 2',
            'name_en'      => 'Swimming Academy Kids — Group 2',
            'age_from'     => 1,
            'age_to'       => 12,
            'duration'     => 55,
            'fee_member'   => 840,
            'fee_nonmember'=> 1680,
        ],
        [
            'code'         => 'SWIM-KIDS-G1',
            'name_ar'      => 'أكاديمية السباحة أطفال — جروب 1',
            'name_en'      => 'Swimming Academy Kids — Group 1',
            'age_from'     => 1,
            'age_to'       => 12,
            'duration'     => 55,
            'fee_member'   => 1080,
            'fee_nonmember'=> 2160,
        ],
        [
            'code'         => 'SWIM-KIDS-PVT',
            'name_ar'      => 'أكاديمية السباحة أطفال — حصة خاصة',
            'name_en'      => 'Swimming Academy Kids — Private',
            'age_from'     => 1,
            'age_to'       => 12,
            'duration'     => 45,
            'fee_member'   => 1600,
            'fee_nonmember'=> 3200,
        ],
        [
            'code'         => 'SWIM-BABY',
            'name_ar'      => 'أكاديمية السباحة — Babys',
            'name_en'      => 'Swimming Academy — Babys',
            'age_from'     => 0,
            'age_to'       => 3,
            'duration'     => 45,
            'fee_member'   => 1200,
            'fee_nonmember'=> 2400,
        ],
    ];

    // Adults Academy (13+ years)
    $adultPrograms = [
        [
            'code'         => 'SWIM-ADULT-G5',
            'name_ar'      => 'أكاديمية السباحة كبار — جروب 5',
            'name_en'      => 'Swimming Academy Adults — Group 5',
            'age_from'     => 13,
            'age_to'       => 65,
            'duration'     => 55,
            'fee_member'   => 600,
            'fee_nonmember'=> 1200,
        ],
        [
            'code'         => 'SWIM-ADULT-G4',
            'name_ar'      => 'أكاديمية السباحة كبار — جروب 4',
            'name_en'      => 'Swimming Academy Adults — Group 4',
            'age_from'     => 13,
            'age_to'       => 65,
            'duration'     => 55,
            'fee_member'   => 700,
            'fee_nonmember'=> 1400,
        ],
        [
            'code'         => 'SWIM-ADULT-G3',
            'name_ar'      => 'أكاديمية السباحة كبار — جروب 3',
            'name_en'      => 'Swimming Academy Adults — Group 3',
            'age_from'     => 13,
            'age_to'       => 65,
            'duration'     => 55,
            'fee_member'   => 800,
            'fee_nonmember'=> 1600,
        ],
        [
            'code'         => 'SWIM-ADULT-G2',
            'name_ar'      => 'أكاديمية السباحة كبار — جروب 2',
            'name_en'      => 'Swimming Academy Adults — Group 2',
            'age_from'     => 13,
            'age_to'       => 65,
            'duration'     => 55,
            'fee_member'   => 900,
            'fee_nonmember'=> 1800,
        ],
        [
            'code'         => 'SWIM-ADULT-G1',
            'name_ar'      => 'أكاديمية السباحة كبار — جروب 1',
            'name_en'      => 'Swimming Academy Adults — Group 1',
            'age_from'     => 13,
            'age_to'       => 65,
            'duration'     => 55,
            'fee_member'   => 1000,
            'fee_nonmember'=> 2000,
        ],
    ];

    $allPrograms = array_merge($kidsPrograms, $adultPrograms);

    foreach ($allPrograms as $prog) {
        $existing = $db->selectOne(
            "SELECT id FROM sa_programs WHERE code = ?",
            [$prog['code']]
        );
        if ($existing) {
            $db->update('sa_programs', [
                'monthly_fee_member'    => $prog['fee_member'],
                'monthly_fee_nonmember' => $prog['fee_nonmember'],
                'session_duration_minutes' => $prog['duration'],
                'updated_at'            => $now,
            ], 'id = ?', [(int) $existing['id']]);
            continue;
        }

        $db->insert('sa_programs', [
            'code'                     => $prog['code'],
            'name_ar'                  => $prog['name_ar'],
            'name_en'                  => $prog['name_en'],
            'discipline_id'            => $disciplineId,
            'program_type'             => 'training',
            'age_from'                 => $prog['age_from'],
            'age_to'                   => $prog['age_to'],
            'session_duration_minutes' => $prog['duration'],
            'sessions_per_week'        => 2,
            'monthly_fee_member'       => $prog['fee_member'],
            'monthly_fee_nonmember'    => $prog['fee_nonmember'],
            'max_capacity'             => 20,
            'min_capacity'             => 1,
            'is_active'                => 1,
            'is_archived'              => 0,
            'created_at'               => $now,
            'updated_at'               => $now,
        ]);
    }
};
