<?php
require_once '../config/headers.php';
require_once '../config/database.php';
require_once '../utils/response.php';

if ($_SERVER['REQUEST_METHOD'] !== 'GET') {
    sendResponse(405, 'error', 'Method not allowed');
}

$database = new Database();
$db = $database->getConnection();

try {
    // Join with holiday_packages to get the package title
    // Added b.total_amount to the SELECT fields
    $query = "
        SELECT 
            b.id, 
            b.package_id, 
            p.title as package_title, 
            b.customer_name, 
            b.customer_email, 
            b.customer_phone, 
            b.travel_date, 
            b.adults, 
            b.children, 
            b.special_requests, 
            b.total_amount,
            b.status, 
            b.created_at 
        FROM package_bookings b
        LEFT JOIN holiday_packages p ON b.package_id = p.id
        ORDER BY b.created_at DESC
    ";
    
    $stmt = $db->prepare($query);
    $stmt->execute();
    
    $bookings = $stmt->fetchAll(PDO::FETCH_ASSOC);

    sendResponse(200, 'success', 'Bookings fetched successfully', $bookings);
} catch (PDOException $e) {
    sendResponse(500, 'error', 'Database error: ' . $e->getMessage());
}
?>