<?php
/**
 * SkyFly Travel - Contact Form Backend (PHP Fallback for Static Export)
 * 
 * Instructions:
 * 1. Upload this file to your server (e.g., as contact.php).
 * 2. Update the ContactForm.tsx component's fetch URL to point to this script.
 * 3. Configure your SMTP settings and reCaptcha Secret Key below.
 */

header("Access-Control-Allow-Origin: *");
header("Access-Control-Allow-Headers: Content-Type");
header("Content-Type: application/json");

// --- CONFIGURATION ---
$secretKey = "YOUR_RECAPTCHA_SECRET_KEY"; // Replace with your Google reCaptcha Secret Key
$toEmail = "bognar.szialrd83@gmail.com";
// ---------------------

if ($_SERVER["REQUEST_METHOD"] === "POST") {
    $data = json_decode(file_get_contents("php://input"), true);
    
    $name = strip_tags($data["name"]);
    $email = filter_var($data["email"], FILTER_SANITIZE_EMAIL);
    $phone = strip_tags($data["phone"]);
    $service = strip_tags($data["service"]);
    $message = strip_tags($data["message"]);
    $recaptcha = $data["recaptcha"];

    // 1. Verify reCaptcha
    $verifyResponse = file_get_contents("https://www.google.com/recaptcha/api/siteverify?secret=$secretKey&response=$recaptcha");
    $responseData = json_decode($verifyResponse);

    if (!$responseData->success) {
        http_response_code(400);
        echo json_encode(["error" => "reCaptcha verification failed"]);
        exit;
    }

    // 2. Prepare HTML Email
    $subject = "Weboldal Üzenet: $name ($service)";
    
    $htmlEmail = "
    <html>
    <body style='font-family: sans-serif; color: #333;'>
        <div style='max-width: 600px; margin: 20px auto; border: 1px solid #ddd; border-radius: 15px; overflow: hidden;'>
            <div style='background: linear-gradient(135deg, #D9A321 0%, #1A1A1A 100%); color: white; padding: 30px; text-align: center;'>
                <h1 style='margin: 0;'>Új üzenet érkezett</h1>
            </div>
            <div style='padding: 30px;'>
                <p><strong>Név:</strong> $name</p>
                <p><strong>Email:</strong> $email</p>
                <p><strong>Telefonszám:</strong> $phone</p>
                <p><strong>Szolgáltatás:</strong> $service</p>
                <hr style='border: 0; border-top: 1px solid #eee; margin: 20px 0;'>
                <p><strong>Üzenet:</strong></p>
                <p style='white-space: pre-wrap; background: #f9f9f9; padding: 15px; border-radius: 10px;'>$message</p>
            </div>
        </div>
    </body>
    </html>
    ";

    $headers = "MIME-Version: 1.0" . "\r\n";
    $headers .= "Content-type:text/html;charset=UTF-8" . "\r\n";
    $headers .= "From: <noreply@skyflytravel.hu>" . "\r\n";

    // 3. Send Email
    if (mail($toEmail, $subject, $htmlEmail, $headers)) {
        echo json_encode(["success" => true]);
    } else {
        http_response_code(500);
        echo json_encode(["error" => "Failed to send email"]);
    }
}
?>
