<?php
// File: contact_handler.php (reCAPTCHA Fix)

// Include the email helper file
require_once 'email_helper.php';

// --- ⭐️ 1. ADD YOUR SECRET KEY HERE ---
$recaptcha_secret_key = '6LfyhQwsAAAAANTnpFElmHnv9bedHN8UbiX_uIBk';
// (Make sure to replace this with the key from your Google reCAPTCHA admin page)

// Check if the form was submitted using POST
if ($_SERVER["REQUEST_METHOD"] == "POST") {

    // --- ⭐️ 2. VERIFY THE reCAPTCHA RESPONSE ---
    $recaptcha_response = $_POST['g-recaptcha-response'] ?? '';
    
    if (empty($recaptcha_response)) {
        // reCAPTCHA box was not checked
        header('Location: contact.php?status=captcha_failed');
        exit();
    }
    
    // Send a cURL request to Google to verify the token
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, "https://www.google.com/recaptcha/api/siteverify");
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query([
        'secret' => $recaptcha_secret_key,
        'response' => $recaptcha_response,
        'remoteip' => $_SERVER['REMOTE_ADDR']
    ]));
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    $response = curl_exec($ch);
    curl_close($ch);
    
    $result = json_decode($response, true);
    
    // Stop if Google says the check failed
    if (!isset($result['success']) || $result['success'] !== true) {
        // Bot detected
        header('Location: contact.php?status=captcha_failed');
        exit();
    }
    // --- END OF reCAPTCHA VERIFICATION ---

    // --- 3. Sanitize and Validate User Input (Spam check passed!) ---
    $fullName = filter_var(trim($_POST["full-name"]), FILTER_SANITIZE_STRING);
    $email = filter_var(trim($_POST["email"]), FILTER_SANITIZE_EMAIL);
    $phone = filter_var(trim($_POST["phone"]), FILTER_SANITIZE_STRING);
    $message = filter_var(trim($_POST["message"]), FILTER_SANITIZE_STRING);

    if (empty($fullName) || !filter_var($email, FILTER_VALIDATE_EMAIL) || empty($message)) {
        header('Location: contact.php?status=error');
        exit();
    }

    // --- 4. Prepare Emails ---
    $adminEmail = 'contact@unitedculturalforum.com'; // Your admin email
    $adminSubject = 'New Contact Form Submission from ' . $fullName;
    $adminBody = "
        <h2>New Message from United Cultural Forum Website</h2>
        <p><strong>Name:</strong> {$fullName}</p>
        <p><strong>Email:</strong> {$email}</p>
        <p><strong>Phone:</strong> {$phone}</p>
        <hr>
        <p><strong>Message:</strong></p>
        <p>{$message}</p>
        <br>
        <p><em>(This message passed the reCAPTCHA check.)</em></p>
    ";

    $userSubject = 'Thank you for contacting United Cultural Forum';
    $userBody = "
        <h2>Thank You for Your Message!</h2>
        <p>Hello {$fullName},</p>
        <p>We have successfully received your message and will get back to you as soon as possible.</p>
        <p>Sincerely,</p>
        <p>The Team at United Cultural Forum</p>
    ";

    // --- 5. Send Emails ---
    $adminEmailSent = sendEmail($adminEmail, $adminSubject, $adminBody);
    $userEmailSent = sendEmail($email, $userSubject, $userBody);

    // --- 6. Redirect based on success ---
    if ($adminEmailSent && $userEmailSent) {
        header('Location: contact.php?status=success');
        exit();
    } else {
        header('Location: contact.php?status=error');
        exit();
    }

} else {
    // If the page is accessed directly, redirect to home
    header('Location: index.php');
    exit();
}
?>