<?php
// Include reCAPTCHA secret key
$secretKey = "6LfYkVIsAAAAACpr40SUTuaS5Smm7imkqjuCA2L6";

// Collect form data
$name = $_POST["name"] ?? '';
$email = $_POST["email"] ?? '';
$phone = $_POST["phone"] ?? '';
$message = $_POST["message"] ?? '';
$recaptchaResponse = $_POST['g-recaptcha-response'] ?? '';

// Verify reCAPTCHA response
$verifyUrl = "https://www.google.com/recaptcha/api/siteverify";
$recaptchaData = [
    'secret' => $secretKey,
    'response' => $recaptchaResponse,
    'remoteip' => $_SERVER['REMOTE_ADDR']
];

$options = [
    'http' => [
        'method' => 'POST',
        'header' => 'Content-type: application/x-www-form-urlencoded',
        'content' => http_build_query($recaptchaData)
    ]
];

$context = stream_context_create($options);
$response = file_get_contents($verifyUrl, false, $context);
$responseKeys = json_decode($response, true);

// If reCAPTCHA validation fails, show an error message
if (!$responseKeys["success"]) {
    echo "<!DOCTYPE html>
    <html lang='en'>
    <head>
        <meta charset='UTF-8'>
        <meta name='viewport' content='width=device-width, initial-scale=1.0'>
        <title>reCAPTCHA Error</title>
        <style>
            body {
                font-family: Arial, sans-serif;
                background-color: #f8f9fa;
                margin: 0;
                padding: 0;
                display: flex;
                align-items: center;
                justify-content: center;
                height: 100vh;
                color: #333;
            }
            .container {
                text-align: center;
                padding: 20px;
                background: white;
                border: 1px solid #ddd;
                box-shadow: 0 4px 8px rgba(0,0,0,0.1);
                border-radius: 5px;
                max-width: 400px;
            }
            .container h1 {
                color: #e63946;
            }
            .container p {
                margin: 15px 0;
            }
            .btn {
                display: inline-block;
                text-decoration: none;
                color: white;
                background-color: #007bff;
                padding: 10px 20px;
                border-radius: 4px;
                margin-top: 20px;
            }
            .btn:hover {
                background-color: #0056b3;
            }
        </style>
    </head>
    <body>
        <div class='container'>
            <h1>Error</h1>
            <p>There was a problem validating the reCAPTCHA. Please try again.</p>
            <a class='btn' href='javascript:history.back()'>Go Back</a>
        </div>
    </body>
    </html>";
    exit;
}

// Proceed with email sending if reCAPTCHA validation is successful
$EmailTo = "gypsiecottage@gmail.com";
$Subject = " Enquiry From Gypsie Cottage";

// Prepare email body text
$Fields = "<table border='2px' style='border-collapse:collapse; width:100%;'>";
$Fields .= "<tr><td style='padding:5px;'>Name</td><td style='padding:5px;'>".$name."</td></tr>";
$Fields .= "<tr><td style='padding:5px;'>Email</td><td style='padding:5px;'>".$email."</td></tr>";
$Fields .= "<tr><td style='padding:5px;'>Phone</td><td style='padding:5px;'>".$phone."</td></tr>";
$Fields .= "<tr><td style='padding:5px;'>Message</td><td style='padding:5px;'>".$message."</td></tr>";
$Fields .= "</table>";

// Send email
$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type:text/html;charset=UTF-8" . "\r\n";
$headers .= 'From: '.$email. "\r\n";

$success = mail($EmailTo, $Subject, $Fields, $headers);

// Redirect to thank-you page
header("Location: thank-you.php");
?>