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

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

if (isset($_GET['user_id'])) {
    $user_id = $_GET['user_id'];

    $query = "SELECT b.*, g.name as ride_name 
              FROM bookings b 
              JOIN games g ON b.game_id = g.id 
              WHERE b.user_id = ? 
              ORDER BY b.id DESC";
    $stmt = $conn->prepare($query);
    $stmt->execute([$user_id]);

    $bookings = array();

    while ($row = $stmt->fetch(PDO::FETCH_ASSOC)){
        $bookings[] = array(
            "id" => $row['id'],
            "user_id" => $row['user_id'],
            "ride_id" => $row['game_id'],
            "ride_name" => $row['ride_name'],
            "date" => $row['date'],
            "persons" => $row['persons'],
            "status" => $row['status']
        );
    }

    echo json_encode($bookings);
} else {
    echo json_encode([]);
}
?>
