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

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

$data = json_decode(file_get_contents("php://input"));

// Basic validation
if (empty($data->package_id) || empty($data->name) || empty($data->email) || empty($data->phone)) {
    sendResponse(400, 'error', 'Missing required fields');
}

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

try {
    $query = "INSERT INTO package_enquiries 
              (package_id, name, email, phone, travel_date, pax_count, message) 
              VALUES (:package_id, :name, :email, :phone, :travel_date, :pax_count, :message)";
    
    $stmt = $db->prepare($query);
    
    $stmt->execute([
        ':package_id' => $data->package_id,
        ':name' => $data->name,
        ':email' => $data->email,
        ':phone' => $data->phone,
        ':travel_date' => !empty($data->travelDate) ? $data->travelDate : null,
        ':pax_count' => $data->paxCount ?? 1,
        ':message' => $data->message ?? null
    ]);

    sendResponse(201, 'success', 'Enquiry submitted successfully');
} catch (PDOException $e) {
    sendResponse(500, 'error', 'Database error: ' . $e->getMessage());
}
?>