<?php
$secretKey = "6LcN1P4sAAAAAE6h83W2gpBlXkWiPWnRxp2pvYM_";

if ($_SERVER["REQUEST_METHOD"] !== "POST") {
  http_response_code(405);
  exit("Method not allowed.");
}

$captchaResponse = $_POST["g-recaptcha-response"] ?? "";

if (empty($captchaResponse)) {
  exit("Please complete the reCAPTCHA.");
}

$verifyResponse = file_get_contents(
  "https://www.google.com/recaptcha/api/siteverify?secret=" .
  urlencode($secretKey) .
  "&response=" .
  urlencode($captchaResponse)
);

$responseData = json_decode($verifyResponse, true);

if (empty($responseData["success"])) {
  exit("reCAPTCHA verification failed. Please try again.");
}

$name = $_POST["Full Name"] ?? "";
$email = $_POST["Email Address"] ?? "";
$organisation = $_POST["Organisation"] ?? "";
$role = $_POST["Role"] ?? "";
$players = $_POST["Number of Players"] ?? "";
$message = $_POST["Message"] ?? "";

$to = "info@trytrack.co.za";
$subject = "New TryTrack Demo Request";

$body = "
New TryTrack demo request:

Full Name: $name
Email: $email
Organisation: $organisation
Role: $role
Number of Players: $players

Message:
$message
";

$headers = "From: info@trytrack.co.za\r\n";
$headers .= "Reply-To: $email\r\n";

mail($to, $subject, $body, $headers);

header("Location: thank-you.html");
exit;
?>