<?php
declare(strict_types=1);

use App\Core\Database;

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

    // Get pool facility
    $pool = $db->selectOne("SELECT id FROM sa_facilities WHERE code = 'POOL-MAIN'", []);
    if (!$pool) {
        return; // Phase_70_001 must run first
    }
    $poolId = (int) $pool['id'];

    // Set grid dimensions: 4 rows × 6 cols = 24 squares
    $db->update('sa_facilities', [
        'pool_grid_rows' => 4,
        'pool_grid_cols' => 6,
        'updated_at'     => $ts,
    ], 'id = ?', [$poolId]);

    // Get some groups for sample assignments
    $groups = $db->select("SELECT id, name_ar, coach_id FROM sa_groups WHERE status = 'active' LIMIT 3", []);

    // Seed zone bookings for today — morning slots
    $sampleSlots = [
        ['start' => '07:00:00', 'end' => '08:00:00'],
        ['start' => '08:00:00', 'end' => '09:00:00'],
        ['start' => '09:00:00', 'end' => '10:00:00'],
    ];

    // Slot 07:00 — Group 1 gets top-left 2x3 block
    if (!empty($groups[0])) {
        $g = $groups[0];
        for ($r = 0; $r < 2; $r++) {
            for ($c = 0; $c < 3; $c++) {
                $db->insert('sa_pool_zone_bookings', [
                    'facility_id'     => $poolId,
                    'booking_date'    => $today,
                    'start_time'      => '07:00:00',
                    'end_time'        => '08:00:00',
                    'zone_row'        => $r,
                    'zone_col'        => $c,
                    'assignment_type' => 'training',
                    'group_id'        => (int) $g['id'],
                    'coach_id'        => $g['coach_id'] ? (int) $g['coach_id'] : null,
                    'label'           => $g['name_ar'],
                    'notes'           => null,
                    'status'          => 'active',
                    'created_by'      => 1,
                    'created_at'      => $ts,
                    'updated_at'      => $ts,
                ]);
            }
        }
    }

    // Slot 07:00 — Maintenance on bottom-right corner
    $db->insert('sa_pool_zone_bookings', [
        'facility_id'     => $poolId,
        'booking_date'    => $today,
        'start_time'      => '07:00:00',
        'end_time'        => '08:00:00',
        'zone_row'        => 3,
        'zone_col'        => 5,
        'assignment_type' => 'maintenance',
        'group_id'        => null,
        'coach_id'        => null,
        'label'           => 'صيانة',
        'notes'           => 'تنظيف فلتر',
        'status'          => 'active',
        'created_by'      => 1,
        'created_at'      => $ts,
        'updated_at'      => $ts,
    ]);

    // Slot 08:00 — Group 2 gets middle strip
    if (!empty($groups[1])) {
        $g = $groups[1];
        for ($c = 0; $c < 6; $c++) {
            $db->insert('sa_pool_zone_bookings', [
                'facility_id'     => $poolId,
                'booking_date'    => $today,
                'start_time'      => '08:00:00',
                'end_time'        => '09:00:00',
                'zone_row'        => 1,
                'zone_col'        => $c,
                'assignment_type' => 'training',
                'group_id'        => (int) $g['id'],
                'coach_id'        => $g['coach_id'] ? (int) $g['coach_id'] : null,
                'label'           => $g['name_ar'],
                'notes'           => null,
                'status'          => 'active',
                'created_by'      => 1,
                'created_at'      => $ts,
                'updated_at'      => $ts,
            ]);
        }
    }

    // Slot 09:00 — Blocked column (col 0)
    for ($r = 0; $r < 4; $r++) {
        $db->insert('sa_pool_zone_bookings', [
            'facility_id'     => $poolId,
            'booking_date'    => $today,
            'start_time'      => '09:00:00',
            'end_time'        => '10:00:00',
            'zone_row'        => $r,
            'zone_col'        => 0,
            'assignment_type' => 'blocked',
            'group_id'        => null,
            'coach_id'        => null,
            'label'           => 'محجوب',
            'notes'           => 'حدث خاص',
            'status'          => 'active',
            'created_by'      => 1,
            'created_at'      => $ts,
            'updated_at'      => $ts,
        ]);
    }

    // Slot 09:00 — Hourly booking in a couple cells
    $db->insert('sa_pool_zone_bookings', [
        'facility_id'     => $poolId,
        'booking_date'    => $today,
        'start_time'      => '09:00:00',
        'end_time'        => '10:00:00',
        'zone_row'        => 2,
        'zone_col'        => 3,
        'assignment_type' => 'hourly',
        'group_id'        => null,
        'coach_id'        => null,
        'label'           => 'حجز خاص',
        'notes'           => null,
        'status'          => 'active',
        'created_by'      => 1,
        'created_at'      => $ts,
        'updated_at'      => $ts,
    ]);
};
