<?php
if ($_SERVER["REQUEST_METHOD"] !== "POST") {
    header("Location: index.html");
    exit;
}

function clean_input($value) {
    return trim(str_replace(array("\r", "\n"), " ", $value ?? ""));
}

function clean_message($value) {
    return trim(str_replace("\r", "", $value ?? ""));
}

function selected_values($value) {
    if (!is_array($value)) {
        return "";
    }
    $cleaned = array_filter(array_map("clean_input", $value));
    return implode(", ", $cleaned);
}

$to = "support@snwealth.in";
$form_type = clean_input($_POST["form_type"] ?? "meeting");
$is_contact_form = $form_type === "contact";

if ($is_contact_form) {
    $first_name = clean_input($_POST["first_name"] ?? "");
    $last_name = clean_input($_POST["last_name"] ?? "");
    $name = trim($first_name . " " . $last_name);
    $phone = clean_input($_POST["phone"] ?? "");
    $email = clean_input($_POST["email"] ?? "");
    $city = clean_input($_POST["city"] ?? "");
    $preferred_time = clean_input($_POST["preferred_time"] ?? "");
    $interests = selected_values($_POST["interests"] ?? array());
    $investment_amount = clean_input($_POST["investment_amount"] ?? "");
    $message = clean_message($_POST["message"] ?? "");

    if ($name === "" || $phone === "" || $email === "") {
        http_response_code(400);
        echo "Please fill first name, last name, phone number and email.";
        exit;
    }

    if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
        http_response_code(400);
        echo "Please enter a valid email address.";
        exit;
    }

    $subject = "SN Wealth Website Lead - Free Consultation";
    $body = "New free consultation request received from SN Wealth contact page.\n\n";
    $body .= "Name: " . $name . "\n";
    $body .= "Phone: " . $phone . "\n";
    $body .= "Email: " . $email . "\n";
    $body .= "City: " . ($city !== "" ? $city : "Not provided") . "\n";
    $body .= "Preferred Call Time: " . ($preferred_time !== "" ? $preferred_time : "Not selected") . "\n";
    $body .= "Interested In: " . ($interests !== "" ? $interests : "Not selected") . "\n";
    $body .= "Approx. Investment Amount: " . ($investment_amount !== "" ? $investment_amount : "Not selected") . "\n";
    $body .= "Message: " . ($message !== "" ? $message : "No message provided") . "\n";
    $reply_to = $email;
    $success_title = "Consultation Request Sent";
    $success_text = "Thank you. Your consultation request has been sent to support@snwealth.in.";
} else {
    $name = clean_input($_POST["name"] ?? "");
    $contact = clean_input($_POST["contact"] ?? "");
    $date = clean_input($_POST["date"] ?? "");
    $time = clean_input($_POST["time"] ?? "");
    $message = clean_message($_POST["message"] ?? "");

    if ($name === "" || $contact === "" || $date === "" || $time === "") {
        http_response_code(400);
        echo "Please fill name, contact number, date and time.";
        exit;
    }

    $subject = "SN Wealth Website Lead - Meeting Request";
    $body = "New meeting request received from SN Wealth website.\n\n";
    $body .= "Name: " . $name . "\n";
    $body .= "Contact Number: " . $contact . "\n";
    $body .= "Preferred Date: " . $date . "\n";
    $body .= "Preferred Time: " . $time . "\n";
    $body .= "Message: " . ($message !== "" ? $message : "No message provided") . "\n";
    $reply_to = "support@snwealth.in";
    $success_title = "Meeting Request Sent";
    $success_text = "Thank you. Your meeting request has been sent to support@snwealth.in.";
}

$body .= "\nSubmitted At: " . date("Y-m-d H:i:s") . "\n";
$body .= "Source Page: " . ($_SERVER["HTTP_REFERER"] ?? "Direct/unknown") . "\n";
$body .= "User IP: " . ($_SERVER["REMOTE_ADDR"] ?? "Unknown") . "\n";

$headers = "From: SN Wealth Website <no-reply@snwealth.in>\r\n";
$headers .= "Reply-To: " . $reply_to . "\r\n";
$headers .= "Return-Path: no-reply@snwealth.in\r\n";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: text/plain; charset=UTF-8\r\n";
$headers .= "X-Mailer: PHP/" . phpversion() . "\r\n";
$headers .= "X-Priority: 1\r\n";
$headers .= "Priority: urgent\r\n";
$headers .= "Importance: High\r\n";

$sent = mail($to, $subject, $body, $headers, "-f no-reply@snwealth.in");
?>
<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <title><?php echo $is_contact_form ? "Consultation Request" : "Meeting Request"; ?> - SN Wealth</title>
  <style>
    body { margin: 0; min-height: 100vh; display: grid; place-items: center; font-family: Arial, sans-serif; background: #f6f8ff; color: #1a2456; }
    .box { width: min(540px, calc(100% - 32px)); background: #fff; border: 1px solid rgba(26,36,86,.12); border-radius: 14px; padding: 32px; box-shadow: 0 20px 60px rgba(26,36,86,.12); text-align: center; }
    h1 { margin: 0 0 10px; color: <?php echo $sent ? "#1a2456" : "#d43a2a"; ?>; font-size: 28px; }
    p { margin: 0 0 22px; line-height: 1.6; color: #687297; }
    a { display: inline-flex; align-items: center; justify-content: center; min-height: 44px; padding: 0 18px; border-radius: 10px; background: #1f4ed8; color: #fff; text-decoration: none; font-weight: 700; }
  </style>
</head>
<body>
  <div class="box">
    <?php if ($sent): ?>
      <h1><?php echo htmlspecialchars($success_title); ?></h1>
      <p><?php echo htmlspecialchars($success_text); ?></p>
    <?php else: ?>
      <h1>Could Not Send</h1>
      <p>Mail server did not accept the request. Please check hosting email settings, PHP mail configuration, SPF, DKIM and DMARC records for snwealth.in.</p>
    <?php endif; ?>
    <a href="index.html">Back to Website</a>
  </div>
</body>
</html>
