<?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

    $programs = [
        // Swimming Teams (Preparatory to General) — 90-120 min sessions
        [
            'code'         => 'SWIM-TEAM-3X',
            'name_ar'      => 'فرق السباحة (تجهيزي–عمومي) — 3 تدريبات أسبوعياً',
            'name_en'      => 'Swimming Teams (Prep–General) — 3x/week',
            'age_from'     => 6,
            'age_to'       => 25,
            'duration'     => 120,
            'sessions_per_week' => 3,
            'fee_member'   => 750,
            'fee_nonmember'=> 1500,
            'type'         => 'competitive',
        ],
        [
            'code'         => 'SWIM-TEAM-4X',
            'name_ar'      => 'فرق السباحة (تجهيزي–عمومي) — 4 تدريبات أسبوعياً',
            'name_en'      => 'Swimming Teams (Prep–General) — 4x/week',
            'age_from'     => 6,
            'age_to'       => 25,
            'duration'     => 120,
            'sessions_per_week' => 4,
            'fee_member'   => 800,
            'fee_nonmember'=> 1600,
            'type'         => 'competitive',
        ],
        [
            'code'         => 'SWIM-TEAM-5X',
            'name_ar'      => 'فرق السباحة (تجهيزي–عمومي) — 5 تدريبات أسبوعياً',
            'name_en'      => 'Swimming Teams (Prep–General) — 5x/week',
            'age_from'     => 6,
            'age_to'       => 25,
            'duration'     => 120,
            'sessions_per_week' => 5,
            'fee_member'   => 850,
            'fee_nonmember'=> 1700,
            'type'         => 'competitive',
        ],
        [
            'code'         => 'SWIM-TEAM-6X',
            'name_ar'      => 'فرق السباحة (تجهيزي–عمومي) — 6 تدريبات أسبوعياً',
            'name_en'      => 'Swimming Teams (Prep–General) — 6x/week',
            'age_from'     => 6,
            'age_to'       => 25,
            'duration'     => 120,
            'sessions_per_week' => 6,
            'fee_member'   => 900,
            'fee_nonmember'=> 1800,
            'type'         => 'competitive',
        ],

        // Masters Swimming Teams (25+) — 60-120 min sessions
        [
            'code'         => 'SWIM-MASTERS-2X',
            'name_ar'      => 'فرق سباحة الأساتذة (Masters) — 2 تدريبات أسبوعياً',
            'name_en'      => 'Masters Swimming — 2x/week',
            'age_from'     => 25,
            'age_to'       => 65,
            'duration'     => 120,
            'sessions_per_week' => 2,
            'fee_member'   => 800,
            'fee_nonmember'=> 1600,
            'type'         => 'competitive',
        ],
        [
            'code'         => 'SWIM-MASTERS-3X',
            'name_ar'      => 'فرق سباحة الأساتذة (Masters) — 3 تدريبات أسبوعياً',
            'name_en'      => 'Masters Swimming — 3x/week',
            'age_from'     => 25,
            'age_to'       => 65,
            'duration'     => 120,
            'sessions_per_week' => 3,
            'fee_member'   => 1050,
            'fee_nonmember'=> 2100,
            'type'         => 'competitive',
        ],
        [
            'code'         => 'SWIM-MASTERS-4X',
            'name_ar'      => 'فرق سباحة الأساتذة (Masters) — 4 تدريبات أسبوعياً',
            'name_en'      => 'Masters Swimming — 4x/week',
            'age_from'     => 25,
            'age_to'       => 65,
            'duration'     => 120,
            'sessions_per_week' => 4,
            'fee_member'   => 1250,
            'fee_nonmember'=> 2500,
            'type'         => 'competitive',
        ],
        [
            'code'         => 'SWIM-MASTERS-5X',
            'name_ar'      => 'فرق سباحة الأساتذة (Masters) — 5 تدريبات أسبوعياً',
            'name_en'      => 'Masters Swimming — 5x/week',
            'age_from'     => 25,
            'age_to'       => 65,
            'duration'     => 120,
            'sessions_per_week' => 5,
            'fee_member'   => 1450,
            'fee_nonmember'=> 2900,
            'type'         => 'competitive',
        ],
    ];

    foreach ($programs 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'],
                'sessions_per_week'     => $prog['sessions_per_week'],
                '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'             => $prog['type'],
            'age_from'                 => $prog['age_from'],
            'age_to'                   => $prog['age_to'],
            'session_duration_minutes' => $prog['duration'],
            'sessions_per_week'        => $prog['sessions_per_week'],
            '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,
        ]);
    }
};
