<?php
declare(strict_types=1);

use App\Core\Database;

return function (Database $db): void {
    $laneRentals = [
        ['50m', 12, 4100],
        ['50m', 8,  2750],
        ['50m', 4,  1400],
        ['50m', 1,  410],
        ['25m', 12, 3000],
        ['25m', 8,  2100],
        ['25m', 4,  1200],
        ['25m', 1,  350],
        ['mix', 24, 6350],
    ];

    foreach ($laneRentals as [$laneType, $sessions, $price]) {
        $existing = $db->selectOne(
            "SELECT id FROM sa_academy_pricing WHERE category = 'lane_rental' AND lane_type = ? AND sessions_per_month = ? LIMIT 1",
            [$laneType, $sessions]
        );
        if ($existing) {
            $db->update('sa_academy_pricing', [
                'price_member'    => $price,
                'price_nonmember' => $price,
                'is_active'       => 1,
                'updated_at'      => date('Y-m-d H:i:s'),
            ], 'id = ?', [(int) $existing['id']]);
        } else {
            $db->insert('sa_academy_pricing', [
                'academy_code'     => 'SWIMMING',
                'academy_name_ar'  => 'أكاديمية السباحة',
                'discipline_code'  => 'SWIMMING',
                'category'         => 'lane_rental',
                'level_name_ar'    => 'حارة ' . $laneType,
                'lane_type'        => $laneType,
                'total_sessions'   => $sessions,
                'sessions_per_month' => $sessions,
                'price_member'     => $price,
                'price_nonmember'  => $price,
                'billing_period'   => 'monthly',
                'is_active'        => 1,
                'effective_from'   => '2025-09-01',
                'sort_order'       => 0,
                'season'           => '2025_2026',
                'created_at'       => date('Y-m-d H:i:s'),
                'updated_at'       => date('Y-m-d H:i:s'),
            ]);
        }
    }

    $sessionCards = [
        [24, 1450],
        [12, 800],
        [8,  580],
        [6,  520],
        [4,  440],
        [1,  140],
    ];

    foreach ($sessionCards as [$sessions, $price]) {
        $existing = $db->selectOne(
            "SELECT id FROM sa_academy_pricing WHERE category = 'session_card' AND total_sessions = ? LIMIT 1",
            [$sessions]
        );
        if ($existing) {
            $db->update('sa_academy_pricing', [
                'price_member'    => $price,
                'price_nonmember' => $price,
                'is_active'       => 1,
                'updated_at'      => date('Y-m-d H:i:s'),
            ], 'id = ?', [(int) $existing['id']]);
        } else {
            $db->insert('sa_academy_pricing', [
                'academy_code'     => 'SWIMMING',
                'academy_name_ar'  => 'أكاديمية السباحة',
                'discipline_code'  => 'SWIMMING',
                'category'         => 'session_card',
                'level_name_ar'    => 'كارت ' . $sessions . ' حصة',
                'total_sessions'   => $sessions,
                'price_member'     => $price,
                'price_nonmember'  => $price,
                'billing_period'   => 'per_card',
                'is_active'        => 1,
                'effective_from'   => '2025-09-01',
                'sort_order'       => 0,
                'season'           => '2025_2026',
                'created_at'       => date('Y-m-d H:i:s'),
                'updated_at'       => date('Y-m-d H:i:s'),
            ]);
        }
    }
};
