<?php
ini_set('display_errors', '1');
error_reporting(E_ALL);

require __DIR__ . '/PHPMailer-master/src/Exception.php';
require __DIR__ . '/PHPMailer-master/src/PHPMailer.php';
require __DIR__ . '/PHPMailer-master/src/SMTP.php';

use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;

// ----------------------
// Form Data Receive
// ----------------------
$fullname   = trim($_POST['fullname'] ?? '');
$email      = trim($_POST['email'] ?? '');
$phone      = trim($_POST['phone'] ?? '');
$cabintype  = trim($_POST['cabintype'] ?? '');
$message    = trim($_POST['message'] ?? '');
$honeypot   = trim($_POST['website_url'] ?? '');   // Honeypot field

// ----------------------
// HONEYPOT CHECK
// ----------------------
if (!empty($honeypot)) {
    die("Spam detected.");
}

// ----------------------
// VALIDATIONS
// ----------------------

// Name validation
if (!preg_match("/^[A-Za-z ]{2,50}$/", $fullname)) {
    die("Invalid name.");
}

// Email validation
if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
    die("Invalid email.");
}

// Phone validation (exact 10 digits)
if (!preg_match("/^[0-9]{10}$/", $phone)) {
    die("Invalid phone number.");
}

$gmailUser  = 'nishantepperporta@gmail.com';
$gmailAppPw = 'wmdldlmrzqcwnype';

$mail = new PHPMailer(true);

try {
    // SMTP Settings
    $mail->isSMTP();
    $mail->Host       = 'smtp.gmail.com';
    $mail->SMTPAuth   = true;
    $mail->Username   = $gmailUser;
    $mail->Password   = $gmailAppPw;
    $mail->SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS;
    $mail->Port       = 587;

    // Sender & Receiver
    $mail->setFrom($gmailUser, 'Website Contact Form');
    $mail->addAddress('nishantepperporta@gmail.com', 'Epper Porta Solutions Pvt. Ltd.');

    // Email Content
    $mail->isHTML(true);
    $mail->Subject = "New Inquiry from Website - $fullname";

    $mail->Body = "
        <h3>New Inquiry Received</h3>
        <p><strong>Name:</strong> $fullname</p>
        <p><strong>Email:</strong> $email</p>
        <p><strong>Phone:</strong> $phone</p>
        <p><strong>Cabin Type:</strong> $cabintype</p>
        <p><strong>Message:</strong><br>$message</p>
    ";

    $mail->AltBody = "New Inquiry:
    Name: $fullname
    Email: $email
    Phone: $phone
    Cabin Type: $cabintype
    Message: $message";

    // Send email
    $mail->send();
    header("Location: thank-you.php");
    exit();

} catch (Exception $e) {
    echo "error: {$mail->ErrorInfo}";
}
?>
