<?php
header('Content-Type: application/json');
include("config/db.php");

$response = ['status' => 'error', 'message' => 'Invalid request.'];

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $name = $_POST['name'] ?? '';
    $email = $_POST['email'] ?? '';
    $mobile = $_POST['mobile'] ?? '';
    $arrival_date = !empty($_POST['arrival_date']) ? $_POST['arrival_date'] : null;
    $num_people = !empty($_POST['num_people']) ? (int)$_POST['num_people'] : null;

    // Using columns that are assumed to exist based on other files.
    $sql = "INSERT INTO contact_messages(name, email, mobile, arrival_date, num_people) VALUES (?, ?, ?, ?, ?)";
    
    $stmt = mysqli_prepare($conn, $sql);
    mysqli_stmt_bind_param($stmt, "ssssi", $name, $email, $mobile, $arrival_date, $num_people);

    if(mysqli_stmt_execute($stmt)){
        $response['status'] = 'success';
        $response['message'] = 'Message Sent Successfully! We will contact you soon.';
    } else {
        $response['message'] = 'Error: Could not save message.';
    }
    mysqli_stmt_close($stmt);
}

mysqli_close($conn);
echo json_encode($response);
?>