// generate_slots.php
require_once 'config.php';
$start = date('Y-m-d');
$end = date('Y-m-d', strtotime('+30 days'));

$chambers = $conn->query("SELECT id FROM chambers WHERE status='active'");
while($ch = $chambers->fetch_assoc()) {
    $chamber_id = $ch['id'];
    $current = new DateTime($start);
    while($current <= new DateTime($end)) {
        $date_str = $current->format('Y-m-d');
        $day = $current->format('w');
        $schedule = $conn->query("SELECT start_time, end_time, slot_duration FROM chamber_schedules 
                                  WHERE chamber_id=$chamber_id AND day_of_week=$day AND status='active'");
        if($schedule->num_rows) {
            $s = $schedule->fetch_assoc();
            $start_t = new DateTime($s['start_time']);
            $end_t = new DateTime($s['end_time']);
            $duration = $s['slot_duration'];
            while($start_t < $end_t) {
                $slot_start = $start_t->format('H:i:s');
                $slot_end = clone $start_t;
                $slot_end->modify("+$duration minutes");
                if($slot_end <= $end_t) {
                    $conn->query("INSERT IGNORE INTO time_slots (chamber_id, slot_date, start_time, end_time) 
                                  VALUES ($chamber_id, '$date_str', '$slot_start', '{$slot_end->format('H:i:s')}')");
                }
                $start_t->modify("+$duration minutes");
            }
        }
        $current->modify('+1 day');
    }
}
echo "Slots generated successfully.";