<?php
header("Content-Type: application/json");

if ($_SERVER["REQUEST_METHOD"] !== "POST") {
    echo json_encode(["status" => "error", "message" => "Invalid request"]);
    exit;
}

// Read normal POST (FormData)
$name     = htmlspecialchars($_POST["first_name"] ?? "");
$phone    = htmlspecialchars($_POST["phone"] ?? "");
$date     = htmlspecialchars($_POST["date_range"] ?? "");
$adults   = htmlspecialchars($_POST["adults"] ?? "0");
$children = htmlspecialchars($_POST["children"] ?? "0");
$rooms    = htmlspecialchars($_POST["rooms"] ?? "0");
$source   = htmlspecialchars($_POST["form_source"] ?? "Website Booking");

// Validation
if (!$name || !$phone || !$date) {
    echo json_encode(["status" => "error", "message" => "Missing fields"]);
    exit;
}

// Email settings
$to = "chozha05@gmail.com, sevenstonestays@gmail.com, sakthisaravana1313@gmail.com";
$subject = "New Booking Request - $source";

$message = "New Booking Received:\n\n";
$message .= "Name: $name\n";
$message .= "Phone: $phone\n";
$message .= "Date: $date\n";
$message .= "Adults: $adults\n";
$message .= "Children: $children\n";
$message .= "Rooms: $rooms\n";
$message .= "Source: $source\n";

$headers = "From: Seven Stones Stays <no-reply@sevenstonesstays.in>\r\n";
$headers .= "Reply-To: $phone\r\n";
$headers .= "Content-Type: text/plain; charset=UTF-8\r\n";

// Send mail
if (mail($to, $subject, $message, $headers)) {
    echo json_encode(["status" => "success"]);
} else {
    echo json_encode(["status" => "error", "message" => "Mail failed"]);
}
