<?php
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);

require_once __DIR__ . "/includes/config.php";

// PHPMailer includes (match folder name EXACTLY)
require_once __DIR__ . "/includes/phpmailer/src/Exception.php";
require_once __DIR__ . "/includes/phpmailer/src/PHPMailer.php";
require_once __DIR__ . "/includes/phpmailer/src/SMTP.php";

$page_title = "Contact | " . $SITE_NAME;

$success = false;
$errors = [];
$sent = false;

function clean_text($v) {
  $v = trim((string)$v);
  return str_replace(["\r", "\n"], " ", $v);
}

function clean_multiline($v) {
  $v = trim((string)$v);
  return str_replace(["\r\n", "\r"], "\n", $v);
}

if ($_SERVER["REQUEST_METHOD"] === "POST") {
  // Basic bot checks
  $honeypot = $_POST["website"] ?? "";
  $formTs = (int)($_POST["form_ts"] ?? 0);

  if ($honeypot !== "") $errors[] = "Spam detected.";
  if ($formTs > 0 && (time() - $formTs) < 3) $errors[] = "Please try again.";

  // Required fields
  $name      = clean_text($_POST["name"] ?? "");
  $email     = clean_text($_POST["email"] ?? "");
  $phone     = clean_text($_POST["phone"] ?? "");
  $eventType = clean_text($_POST["event_type"] ?? "");

  if ($name === "") $errors[] = "Name is required.";
  if (!filter_var($email, FILTER_VALIDATE_EMAIL)) $errors[] = "A valid email is required.";
  if ($phone === "") $errors[] = "Phone is required.";
  if ($eventType === "") $errors[] = "Event type is required.";

  // Optional fields
  $eventDate  = clean_text($_POST["event_date"] ?? "");
  $eventCity  = clean_text($_POST["event_city"] ?? "");
  $guestCount = clean_text($_POST["guest_count"] ?? "");
  $heardFrom  = clean_text($_POST["heard_from"] ?? "");
  $details    = clean_multiline($_POST["details"] ?? "");
  $okToText   = (($_POST["ok_to_text"] ?? "") === "yes") ? "Yes" : "No";

  if (!$errors) {
    $subject = $CONTACT_SUBJECT_PREFIX . " — " . $name;

    $body =
      "New Quote Request\n\n"
      . "Name: {$name}\n"
      . "Email: {$email}\n"
      . "Phone: {$phone}\n"
      . "Event Type: {$eventType}\n"
      . "Event Date: {$eventDate}\n"
      . "City / Venue: {$eventCity}\n"
      . "Estimated Guest Count: {$guestCount}\n"
      . "Heard From: {$heardFrom}\n"
      . "OK to text: {$okToText}\n\n"
      . "Details:\n{$details}\n";

    try {
      $mail = new \PHPMailer\PHPMailer\PHPMailer(true);

      $mail->isSMTP();
      $mail->Host       = $SMTP_HOST;
      $mail->SMTPAuth   = true;
      $mail->Username   = $SMTP_USER;
      $mail->Password   = $SMTP_PASS;

      $mail->SMTPSecure = $SMTP_ENCRYPTION; // "tls" (587) or "ssl" (465)
      $mail->Port       = $SMTP_PORT;

      $mail->CharSet = "UTF-8";

      $mail->setFrom($CONTACT_FROM_EMAIL, $SITE_NAME);
      $mail->addAddress($CONTACT_TO_EMAIL);
      $mail->addReplyTo($email, $name);

      $mail->Subject = $subject;
      $mail->Body    = $body;

      $sent = $mail->send();

      if ($sent) {
        $success = true;
        $_POST = [];
      } else {
        $errors[] = "Message could not be sent (SMTP returned false).";
      }

    } catch (\PHPMailer\PHPMailer\Exception $e) {
      $errors[] = "Mailer error: " . $e->getMessage();
    }

    // Log result
    file_put_contents(
      __DIR__ . "/mail_log.txt",
      date("c") . " sent=" . ($sent ? "1" : "0") . " to={$CONTACT_TO_EMAIL} replyto={$email}\n",
      FILE_APPEND
    );
  }
}

include __DIR__ . "/includes/head.php";
include __DIR__ . "/includes/nav.php";
require __DIR__ . "/includes/pages/contact-content.php";
include __DIR__ . "/includes/footer.php";