<?php
include 'db.php';

header('Content-Type: application/json');

if ($_SERVER['REQUEST_METHOD'] == 'POST') {
    $fullName = $_POST['fullName'] ?? '';
    $email = $_POST['email'] ?? '';
    $phoneNumber = $_POST['phoneNumber'] ?? '';
    $comment = $_POST['comment'] ?? '';

    if (empty($fullName) || empty($email) || empty($phoneNumber) || empty($comment)) {
        echo json_encode(['success' => false, 'message' => 'Please fill in all required fields.']);
        exit;
    }

    $stmt = $conn->prepare("INSERT INTO contact_messages (full_name, email, phone_number, comment) VALUES (?, ?, ?, ?)");
    $stmt->bind_param("ssss", $fullName, $email, $phoneNumber, $comment);

    if ($stmt->execute()) {
        echo json_encode(['success' => true, 'message' => 'Message sent successfully!']);
    } else {
        echo json_encode(['success' => false, 'message' => 'Error: ' . $stmt->error]);
    }

    $stmt->close();
    $conn->close();
} else {
    echo json_encode(['success' => false, 'message' => 'Invalid request method.']);
}
?>