<?php
declare(strict_types=1);

use App\Core\Database;

return function (Database $db): void {
    $discipline = $db->selectOne(
        "SELECT id FROM sa_disciplines WHERE code = 'SWIMMING' AND is_archived = 0 LIMIT 1"
    );
    if (!$discipline) {
        $discipline = $db->selectOne(
            "SELECT id FROM sa_disciplines WHERE name_ar LIKE '%سباحة%' AND is_archived = 0 LIMIT 1"
        );
    }
    $disciplineId = $discipline ? (int) $discipline['id'] : 1;

    $existing = $db->selectOne(
        "SELECT id FROM sa_programs WHERE code = 'SWIM-LANE-RENTAL'"
    );
    if (!$existing) {
        $db->insert('sa_programs', [
            'code'                     => 'SWIM-LANE-RENTAL',
            'name_ar'                  => 'إيجار حارات السباحة',
            'name_en'                  => 'Pool Lane Rental',
            'discipline_id'            => $disciplineId,
            'program_type'             => 'rental',
            'session_duration_minutes' => 60,
            'sessions_per_week'        => 0,
            'monthly_fee_member'       => 0,
            'monthly_fee_nonmember'    => 0,
            'is_active'                => 1,
            'created_at'               => date('Y-m-d H:i:s'),
            'updated_at'               => date('Y-m-d H:i:s'),
        ]);
    }

    $existing2 = $db->selectOne(
        "SELECT id FROM sa_programs WHERE code = 'SWIM-CARDS'"
    );
    if (!$existing2) {
        $db->insert('sa_programs', [
            'code'                     => 'SWIM-CARDS',
            'name_ar'                  => 'كروت حصص السباحة',
            'name_en'                  => 'Pool Session Cards',
            'discipline_id'            => $disciplineId,
            'program_type'             => 'rental',
            'session_duration_minutes' => 60,
            'sessions_per_week'        => 0,
            'monthly_fee_member'       => 0,
            'monthly_fee_nonmember'    => 0,
            'is_active'                => 1,
            'created_at'               => date('Y-m-d H:i:s'),
            'updated_at'               => date('Y-m-d H:i:s'),
        ]);
    }
};
