<?php
/**
 * Reservation Details API
 * Fetch detailed information about a specific reservation
 */

require_once __DIR__ . '/../config/session.php';
require_once __DIR__ . '/../config/database.php';
require_once __DIR__ . '/../config/helpers.php';

header('Content-Type: application/json');

// Check if user is logged in
if (!isLoggedIn()) {
    http_response_code(401);
    echo json_encode([
        'success' => false,
        'message' => 'Not authenticated'
    ]);
    exit();
}

$userId = getCurrentUserId();
$reservationId = isset($_GET['id']) ? intval($_GET['id']) : 0;

if (!$reservationId) {
    http_response_code(400);
    echo json_encode([
        'success' => false,
        'message' => 'Reservation ID is required'
    ]);
    exit();
}

$dbHelper = new DBHelper($conn);

// Get reservation details
$sql = "SELECT r.*, ro.room_number, ro.room_type, ro.image_url, ro.price_per_night
        FROM reservations r
        LEFT JOIN rooms ro ON r.room_id = ro.id
        WHERE r.id = ? AND r.user_id = ?";

$stmt = $conn->prepare($sql);
$stmt->bind_param('ii', $reservationId, $userId);
$stmt->execute();
$result = $stmt->get_result();

if ($result->num_rows === 0) {
    http_response_code(404);
    echo json_encode([
        'success' => false,
        'message' => 'Reservation not found'
    ]);
    exit();
}

$reservation = $result->fetch_assoc();

echo json_encode([
    'success' => true,
    'reservation' => $reservation
]);
?>
