<?php
header("Access-Control-Allow-Origin: *");
header("Content-Type: application/json; charset=UTF-8");
header("Access-Control-Allow-Methods: POST");

include_once '../config/database.php';

if ($_SERVER['REQUEST_METHOD'] == 'POST') {
    $user_id = $_POST['user_id'];
    $ride_id = $_POST['ride_id'];
    $date = $_POST['date'];
    $persons = $_POST['persons'];
    
    // Generate a random ticket code
    $ticket_code = "FL-" . strtoupper(bin2hex(random_bytes(3)));

    $query = "INSERT INTO bookings (user_id, game_id, date, persons, status, ticket_code) VALUES (?, ?, ?, ?, ?, ?)";
    $stmt = $conn->prepare($query);
    
    if ($stmt->execute([$user_id, $ride_id, $date, $persons, 'Pending', $ticket_code])) {
        echo json_encode(["success" => true, "message" => "تم الحجز بنجاح"]);
    } else {
        echo json_encode(["success" => false, "message" => "فشل عملية الحجز"]);
    }
}
?>
