<?php
if (!defined('HOSTIVA_API')) exit;
require_permission('guest', 'viewer', 'admin', 'super_admin'); // Allow guests to access

$user = current_user();

// Fetch the active booking for this guest
$stmt = $pdo->prepare("
    SELECT b.*, p.name as property_name, p.address as property_address, p.image_url
    FROM bookings b
    JOIN properties p ON b.property_id = p.id
    WHERE b.guest_user_id = ? AND b.status IN ('confirmed', 'checked_in')
    ORDER BY b.check_in ASC LIMIT 1
");
$stmt->execute([$user['id']]);
$booking = $stmt->fetch();

if (!$booking) {
    echo json_encode(['success' => false, 'message' => 'No active reservations found.']);
    exit;
}

echo json_encode(['success' => true, 'data' => $booking]);