<?php
declare(strict_types=1);

use App\Core\Database;

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

    // Helper to resolve discipline id by code
    $disciplineId = function (string $code) use ($db): ?int {
        $row = $db->selectOne("SELECT id FROM sport_disciplines WHERE code = ?", [$code]);
        return $row ? (int) $row['id'] : null;
    };

    $facilities = [
        // ── Football pitches ──
        [
            'code' => 'football_legal', 'name_ar' => 'ملعب كرة قدم قانوني', 'name_en' => 'Legal Football Pitch',
            'facility_type' => 'pitch', 'location' => 'outdoor', 'capacity' => 22,
            'hourly_rate_member' => '500.00', 'hourly_rate_nonmember' => '1000.00',
            'hourly_rate_member_pm' => '600.00', 'hourly_rate_nonmember_pm' => '1200.00',
            'discipline' => 'football', 'config_json' => null,
        ],
        [
            'code' => 'football_futsal', 'name_ar' => 'ملعب كرة صالات', 'name_en' => 'Futsal Pitch',
            'facility_type' => 'pitch', 'location' => 'indoor', 'capacity' => 10,
            'hourly_rate_member' => '300.00', 'hourly_rate_nonmember' => '600.00',
            'hourly_rate_member_pm' => '360.00', 'hourly_rate_nonmember_pm' => '720.00',
            'discipline' => 'futsal', 'config_json' => null,
        ],

        // ── Multi-purpose courts ──
        [
            'code' => 'court_multi_1', 'name_ar' => 'ملعب متعدد الأغراض 1', 'name_en' => 'Multi-Purpose Court 1',
            'facility_type' => 'court', 'location' => 'outdoor', 'capacity' => 20,
            'hourly_rate_member' => '200.00', 'hourly_rate_nonmember' => '400.00',
            'hourly_rate_member_pm' => '240.00', 'hourly_rate_nonmember_pm' => '480.00',
            'discipline' => null, 'config_json' => null,
        ],
        [
            'code' => 'court_multi_2', 'name_ar' => 'ملعب متعدد الأغراض 2', 'name_en' => 'Multi-Purpose Court 2',
            'facility_type' => 'court', 'location' => 'outdoor', 'capacity' => 20,
            'hourly_rate_member' => '200.00', 'hourly_rate_nonmember' => '400.00',
            'hourly_rate_member_pm' => '240.00', 'hourly_rate_nonmember_pm' => '480.00',
            'discipline' => null, 'config_json' => null,
        ],

        // ── Tennis courts ──
        [
            'code' => 'tennis_1', 'name_ar' => 'ملعب تنس 1', 'name_en' => 'Tennis Court 1',
            'facility_type' => 'court', 'location' => 'outdoor', 'capacity' => 4,
            'hourly_rate_member' => '150.00', 'hourly_rate_nonmember' => '300.00',
            'hourly_rate_member_pm' => '180.00', 'hourly_rate_nonmember_pm' => '360.00',
            'discipline' => 'tennis', 'config_json' => null,
        ],
        [
            'code' => 'tennis_2', 'name_ar' => 'ملعب تنس 2', 'name_en' => 'Tennis Court 2',
            'facility_type' => 'court', 'location' => 'outdoor', 'capacity' => 4,
            'hourly_rate_member' => '150.00', 'hourly_rate_nonmember' => '300.00',
            'hourly_rate_member_pm' => '180.00', 'hourly_rate_nonmember_pm' => '360.00',
            'discipline' => 'tennis', 'config_json' => null,
        ],

        // ── Padel courts ──
        [
            'code' => 'padel_1', 'name_ar' => 'ملعب بادل 1', 'name_en' => 'Padel Court 1',
            'facility_type' => 'court', 'location' => 'outdoor', 'capacity' => 4,
            'hourly_rate_member' => '200.00', 'hourly_rate_nonmember' => '400.00',
            'hourly_rate_member_pm' => '240.00', 'hourly_rate_nonmember_pm' => '480.00',
            'discipline' => 'padel', 'config_json' => null,
        ],
        [
            'code' => 'padel_2', 'name_ar' => 'ملعب بادل 2', 'name_en' => 'Padel Court 2',
            'facility_type' => 'court', 'location' => 'outdoor', 'capacity' => 4,
            'hourly_rate_member' => '200.00', 'hourly_rate_nonmember' => '400.00',
            'hourly_rate_member_pm' => '240.00', 'hourly_rate_nonmember_pm' => '480.00',
            'discipline' => 'padel', 'config_json' => null,
        ],
        [
            'code' => 'padel_3', 'name_ar' => 'ملعب بادل 3', 'name_en' => 'Padel Court 3',
            'facility_type' => 'court', 'location' => 'outdoor', 'capacity' => 4,
            'hourly_rate_member' => '200.00', 'hourly_rate_nonmember' => '400.00',
            'hourly_rate_member_pm' => '240.00', 'hourly_rate_nonmember_pm' => '480.00',
            'discipline' => 'padel', 'config_json' => null,
        ],

        // ── Squash courts (indoor) ──
        [
            'code' => 'squash_indoor_1', 'name_ar' => 'ملعب اسكواش داخلي 1', 'name_en' => 'Indoor Squash Court 1',
            'facility_type' => 'court', 'location' => 'indoor', 'capacity' => 2,
            'hourly_rate_member' => '100.00', 'hourly_rate_nonmember' => '200.00',
            'hourly_rate_member_pm' => '120.00', 'hourly_rate_nonmember_pm' => '240.00',
            'discipline' => 'squash', 'config_json' => null,
        ],
        [
            'code' => 'squash_indoor_2', 'name_ar' => 'ملعب اسكواش داخلي 2', 'name_en' => 'Indoor Squash Court 2',
            'facility_type' => 'court', 'location' => 'indoor', 'capacity' => 2,
            'hourly_rate_member' => '100.00', 'hourly_rate_nonmember' => '200.00',
            'hourly_rate_member_pm' => '120.00', 'hourly_rate_nonmember_pm' => '240.00',
            'discipline' => 'squash', 'config_json' => null,
        ],
        [
            'code' => 'squash_indoor_3', 'name_ar' => 'ملعب اسكواش داخلي 3', 'name_en' => 'Indoor Squash Court 3',
            'facility_type' => 'court', 'location' => 'indoor', 'capacity' => 2,
            'hourly_rate_member' => '100.00', 'hourly_rate_nonmember' => '200.00',
            'hourly_rate_member_pm' => '120.00', 'hourly_rate_nonmember_pm' => '240.00',
            'discipline' => 'squash', 'config_json' => null,
        ],
        [
            'code' => 'squash_indoor_4', 'name_ar' => 'ملعب اسكواش داخلي 4', 'name_en' => 'Indoor Squash Court 4',
            'facility_type' => 'court', 'location' => 'indoor', 'capacity' => 2,
            'hourly_rate_member' => '100.00', 'hourly_rate_nonmember' => '200.00',
            'hourly_rate_member_pm' => '120.00', 'hourly_rate_nonmember_pm' => '240.00',
            'discipline' => 'squash', 'config_json' => null,
        ],

        // ── Squash courts (outdoor) ──
        [
            'code' => 'squash_outdoor_1', 'name_ar' => 'ملعب اسكواش خارجي 1', 'name_en' => 'Outdoor Squash Court 1',
            'facility_type' => 'court', 'location' => 'outdoor', 'capacity' => 2,
            'hourly_rate_member' => '80.00', 'hourly_rate_nonmember' => '160.00',
            'hourly_rate_member_pm' => '96.00', 'hourly_rate_nonmember_pm' => '192.00',
            'discipline' => 'squash', 'config_json' => null,
        ],
        [
            'code' => 'squash_outdoor_2', 'name_ar' => 'ملعب اسكواش خارجي 2', 'name_en' => 'Outdoor Squash Court 2',
            'facility_type' => 'court', 'location' => 'outdoor', 'capacity' => 2,
            'hourly_rate_member' => '80.00', 'hourly_rate_nonmember' => '160.00',
            'hourly_rate_member_pm' => '96.00', 'hourly_rate_nonmember_pm' => '192.00',
            'discipline' => 'squash', 'config_json' => null,
        ],

        // ── Combat halls ──
        [
            'code' => 'combat_hall_1', 'name_ar' => 'قاعة فنون قتالية 1', 'name_en' => 'Combat Hall 1',
            'facility_type' => 'hall', 'location' => 'indoor', 'capacity' => 30,
            'hourly_rate_member' => '250.00', 'hourly_rate_nonmember' => '500.00',
            'hourly_rate_member_pm' => '300.00', 'hourly_rate_nonmember_pm' => '600.00',
            'discipline' => 'karate', 'config_json' => null,
        ],
        [
            'code' => 'combat_hall_2', 'name_ar' => 'قاعة فنون قتالية 2', 'name_en' => 'Combat Hall 2',
            'facility_type' => 'hall', 'location' => 'indoor', 'capacity' => 30,
            'hourly_rate_member' => '250.00', 'hourly_rate_nonmember' => '500.00',
            'hourly_rate_member_pm' => '300.00', 'hourly_rate_nonmember_pm' => '600.00',
            'discipline' => 'kung_fu', 'config_json' => null,
        ],

        // ── Bowling lanes ──
        [
            'code' => 'bowling_lane_1', 'name_ar' => 'حارة بولينج 1', 'name_en' => 'Bowling Lane 1',
            'facility_type' => 'lane', 'location' => 'indoor', 'capacity' => 6,
            'hourly_rate_member' => '50.00', 'hourly_rate_nonmember' => '100.00',
            'hourly_rate_member_pm' => '60.00', 'hourly_rate_nonmember_pm' => '120.00',
            'discipline' => 'bowling', 'config_json' => json_encode(['kids_only' => true], JSON_UNESCAPED_UNICODE),
        ],
        [
            'code' => 'bowling_lane_2', 'name_ar' => 'حارة بولينج 2', 'name_en' => 'Bowling Lane 2',
            'facility_type' => 'lane', 'location' => 'indoor', 'capacity' => 6,
            'hourly_rate_member' => '50.00', 'hourly_rate_nonmember' => '100.00',
            'hourly_rate_member_pm' => '60.00', 'hourly_rate_nonmember_pm' => '120.00',
            'discipline' => 'bowling', 'config_json' => null,
        ],
        [
            'code' => 'bowling_lane_3', 'name_ar' => 'حارة بولينج 3', 'name_en' => 'Bowling Lane 3',
            'facility_type' => 'lane', 'location' => 'indoor', 'capacity' => 6,
            'hourly_rate_member' => '50.00', 'hourly_rate_nonmember' => '100.00',
            'hourly_rate_member_pm' => '60.00', 'hourly_rate_nonmember_pm' => '120.00',
            'discipline' => 'bowling', 'config_json' => null,
        ],
        [
            'code' => 'bowling_lane_4', 'name_ar' => 'حارة بولينج 4', 'name_en' => 'Bowling Lane 4',
            'facility_type' => 'lane', 'location' => 'indoor', 'capacity' => 6,
            'hourly_rate_member' => '50.00', 'hourly_rate_nonmember' => '100.00',
            'hourly_rate_member_pm' => '60.00', 'hourly_rate_nonmember_pm' => '120.00',
            'discipline' => 'bowling', 'config_json' => null,
        ],
        [
            'code' => 'bowling_lane_5', 'name_ar' => 'حارة بولينج 5', 'name_en' => 'Bowling Lane 5',
            'facility_type' => 'lane', 'location' => 'indoor', 'capacity' => 6,
            'hourly_rate_member' => '50.00', 'hourly_rate_nonmember' => '100.00',
            'hourly_rate_member_pm' => '60.00', 'hourly_rate_nonmember_pm' => '120.00',
            'discipline' => 'bowling', 'config_json' => null,
        ],
        [
            'code' => 'bowling_lane_6', 'name_ar' => 'حارة بولينج 6', 'name_en' => 'Bowling Lane 6',
            'facility_type' => 'lane', 'location' => 'indoor', 'capacity' => 6,
            'hourly_rate_member' => '50.00', 'hourly_rate_nonmember' => '100.00',
            'hourly_rate_member_pm' => '60.00', 'hourly_rate_nonmember_pm' => '120.00',
            'discipline' => 'bowling', 'config_json' => null,
        ],

        // ── Table tennis tables ──
        [
            'code' => 'table_tennis_1', 'name_ar' => 'طاولة تنس طاولة 1', 'name_en' => 'Table Tennis 1',
            'facility_type' => 'table', 'location' => 'indoor', 'capacity' => 4,
            'hourly_rate_member' => '40.00', 'hourly_rate_nonmember' => '80.00',
            'hourly_rate_member_pm' => '48.00', 'hourly_rate_nonmember_pm' => '96.00',
            'discipline' => 'table_tennis', 'config_json' => null,
        ],
        [
            'code' => 'table_tennis_2', 'name_ar' => 'طاولة تنس طاولة 2', 'name_en' => 'Table Tennis 2',
            'facility_type' => 'table', 'location' => 'indoor', 'capacity' => 4,
            'hourly_rate_member' => '40.00', 'hourly_rate_nonmember' => '80.00',
            'hourly_rate_member_pm' => '48.00', 'hourly_rate_nonmember_pm' => '96.00',
            'discipline' => 'table_tennis', 'config_json' => null,
        ],

        // ── Billiards tables ──
        [
            'code' => 'billiards_1', 'name_ar' => 'طاولة بلياردو 1', 'name_en' => 'Billiards Table 1',
            'facility_type' => 'table', 'location' => 'indoor', 'capacity' => 4,
            'hourly_rate_member' => '60.00', 'hourly_rate_nonmember' => '120.00',
            'hourly_rate_member_pm' => '72.00', 'hourly_rate_nonmember_pm' => '144.00',
            'discipline' => 'billiards', 'config_json' => null,
        ],
        [
            'code' => 'billiards_2', 'name_ar' => 'طاولة بلياردو 2', 'name_en' => 'Billiards Table 2',
            'facility_type' => 'table', 'location' => 'indoor', 'capacity' => 4,
            'hourly_rate_member' => '60.00', 'hourly_rate_nonmember' => '120.00',
            'hourly_rate_member_pm' => '72.00', 'hourly_rate_nonmember_pm' => '144.00',
            'discipline' => 'billiards', 'config_json' => null,
        ],

        // ── PlayStation devices ──
        [
            'code' => 'ps_1', 'name_ar' => 'جهاز بلايستيشن 1', 'name_en' => 'PlayStation 1',
            'facility_type' => 'device', 'location' => 'indoor', 'capacity' => 2,
            'hourly_rate_member' => '30.00', 'hourly_rate_nonmember' => '60.00',
            'hourly_rate_member_pm' => '36.00', 'hourly_rate_nonmember_pm' => '72.00',
            'discipline' => 'playstation', 'config_json' => null,
        ],
        [
            'code' => 'ps_2', 'name_ar' => 'جهاز بلايستيشن 2', 'name_en' => 'PlayStation 2',
            'facility_type' => 'device', 'location' => 'indoor', 'capacity' => 2,
            'hourly_rate_member' => '30.00', 'hourly_rate_nonmember' => '60.00',
            'hourly_rate_member_pm' => '36.00', 'hourly_rate_nonmember_pm' => '72.00',
            'discipline' => 'playstation', 'config_json' => null,
        ],
        [
            'code' => 'ps_3', 'name_ar' => 'جهاز بلايستيشن 3', 'name_en' => 'PlayStation 3',
            'facility_type' => 'device', 'location' => 'indoor', 'capacity' => 2,
            'hourly_rate_member' => '30.00', 'hourly_rate_nonmember' => '60.00',
            'hourly_rate_member_pm' => '36.00', 'hourly_rate_nonmember_pm' => '72.00',
            'discipline' => 'playstation', 'config_json' => null,
        ],

        // ── Tracks ──
        [
            'code' => 'running_track', 'name_ar' => 'مضمار جري', 'name_en' => 'Running Track',
            'facility_type' => 'track', 'location' => 'outdoor', 'capacity' => 20,
            'hourly_rate_member' => '0.00', 'hourly_rate_nonmember' => '50.00',
            'hourly_rate_member_pm' => '0.00', 'hourly_rate_nonmember_pm' => '60.00',
            'discipline' => null, 'config_json' => null,
        ],
        [
            'code' => 'cycling_track', 'name_ar' => 'مسار دراجات/تزلج', 'name_en' => 'Cycling / Skating Track',
            'facility_type' => 'track', 'location' => 'outdoor', 'capacity' => 15,
            'hourly_rate_member' => '0.00', 'hourly_rate_nonmember' => '50.00',
            'hourly_rate_member_pm' => '0.00', 'hourly_rate_nonmember_pm' => '60.00',
            'discipline' => 'skating', 'config_json' => null,
        ],

        // ── Pool ──
        [
            'code' => 'swimming_pool', 'name_ar' => 'حمام سباحة', 'name_en' => 'Swimming Pool',
            'facility_type' => 'pool', 'location' => 'outdoor', 'capacity' => 50,
            'hourly_rate_member' => '75.00', 'hourly_rate_nonmember' => '150.00',
            'hourly_rate_member_pm' => '90.00', 'hourly_rate_nonmember_pm' => '180.00',
            'discipline' => 'swimming', 'config_json' => null,
        ],
    ];

    foreach ($facilities as $f) {
        $existing = $db->selectOne("SELECT id FROM facilities WHERE code = ?", [$f['code']]);
        if ($existing) {
            continue;
        }

        $linkedId = null;
        if ($f['discipline'] !== null) {
            $linkedId = $disciplineId($f['discipline']);
        }

        $db->insert('facilities', [
            'code'                     => $f['code'],
            'name_ar'                  => $f['name_ar'],
            'name_en'                  => $f['name_en'],
            'facility_type'            => $f['facility_type'],
            'location'                 => $f['location'],
            'capacity'                 => $f['capacity'],
            'hourly_rate_member'       => $f['hourly_rate_member'],
            'hourly_rate_nonmember'    => $f['hourly_rate_nonmember'],
            'hourly_rate_member_pm'    => $f['hourly_rate_member_pm'],
            'hourly_rate_nonmember_pm' => $f['hourly_rate_nonmember_pm'],
            'linked_discipline_id'     => $linkedId,
            'config_json'              => $f['config_json'],
            'is_active'                => 1,
            'created_at'               => $ts,
            'updated_at'               => $ts,
        ]);
    }
};
