<?php
include 'includes/auth.php';
include 'db.php';

// ===============================
// GET BOOKING ID
// ===============================
$booking_id = isset($_GET['booking_id']) ? intval($_GET['booking_id']) : 0;
if ($booking_id <= 0) {
    echo "<script>alert('Invalid booking ID'); window.location='conference_bookings.php';</script>";
    exit;
}

// ===============================
// FETCH BOOKING
// ===============================
$stmt = $conn->prepare("SELECT * FROM conference_bookings WHERE id=?");
$stmt->bind_param("i", $booking_id);
$stmt->execute();
$result = $stmt->get_result();
$booking = $result->fetch_assoc();

if (!$booking) {
    echo "<script>alert('Booking not found'); window.location='conference_bookings.php';</script>";
    exit;
}

// ===============================
// CHECK IF START IS ALLOWED
// ===============================
$now = new DateTime();
$start_date = new DateTime($booking['booking_date']);
$end_date = clone $start_date;
$end_date->modify('+'.($booking['num_days']-1).' days');

if ($now < $start_date) {
    echo "<script>alert('Conference cannot start before the start date.'); window.location='conference_bookings.php';</script>";
    exit;
}

// Only allow starting if status is CONFIRMED
if ($booking['status'] != 'CONFIRMED') {
    echo "<script>alert('Conference must be CONFIRMED before starting.'); window.location='conference_bookings.php';</script>";
    exit;
}

// ===============================
// UPDATE STATUS TO STARTED
// ===============================
$update_stmt = $conn->prepare("UPDATE conference_bookings SET status='ONGOING', started_at=NOW() WHERE id=?");
$update_stmt->bind_param("i", $booking_id);
$update_stmt->execute();

// Optional: you can log this action in a separate table if needed

echo "<script>
        alert('🚀 Conference #{$booking_id} started successfully!');
        window.location='view_conference_booking.php?booking_id={$booking_id}';
      </script>";
exit;
?>
