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

// Only allow GET requests for fetching data
if ($_SERVER['REQUEST_METHOD'] !== 'GET') {
    sendResponse(405, 'error', 'Method not allowed');
}

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

try {
    // Fetch all enquiries and join with holiday_packages to get the package title
    $query = "
        SELECT 
            e.id, 
            e.package_id, 
            p.title as package_title, 
            e.name as customer_name, 
            e.email as customer_email, 
            e.phone as customer_phone, 
            e.travel_date, 
            e.pax_count, 
            e.message, 
            e.status, 
            e.created_at 
        FROM package_enquiries e
        LEFT JOIN holiday_packages p ON e.package_id = p.id
        ORDER BY e.created_at DESC
    ";
    
    $stmt = $db->prepare($query);
    $stmt->execute();
    
    // Fetch data as associative array
    $enquiries = $stmt->fetchAll(PDO::FETCH_ASSOC);

    // Return success response with data
    sendResponse(200, 'success', 'Enquiries fetched successfully', $enquiries);

} catch (PDOException $e) {
    // Handle database errors
    sendResponse(500, 'error', 'Database error: ' . $e->getMessage());
}
?>