<?php
session_start();
include 'includes/smtp-mailer.php';

if ($_SERVER["REQUEST_METHOD"] == "POST") {
    // 1. Honeypot Protection
    if (!empty($_POST["full_name_honeypot"])) {
        // Log bot attempt if desired
        exit("Spam detected."); 
    }

    // 2. Rate Limiting (1 submission per 60 seconds per session)
    $current_time = time();
    if (isset($_SESSION['last_submission_time']) && ($current_time - $_SESSION['last_submission_time']) < 30) {
        http_response_code(429);
        echo "Too many requests. Please wait a moment before trying again.";
        exit;
    }

    $name = strip_tags(trim($_POST["name"]));
    $email = filter_var(trim($_POST["email"]), FILTER_SANITIZE_EMAIL);
    $phone = strip_tags(trim($_POST["phone"]));
    $message = strip_tags(trim($_POST["message"]));
    
    // Country Metadata (if available from intl-tel-input)
    $country_name = isset($_POST["country_name"]) ? strip_tags(trim($_POST["country_name"])) : "";
    $country_code = isset($_POST["country_code"]) ? strip_tags(trim($_POST["country_code"])) : "";

    // Package Metadata
    $leadprice = isset($_POST["leadprice"]) ? strip_tags(trim($_POST["leadprice"])) : "";
    $serviii = isset($_POST["serviii"]) ? strip_tags(trim($_POST["serviii"])) : "";

    // Determine which form was submitted
    $form_type = "Contact Form";
    if (isset($_POST["submit_quote"])) {
        $form_type = "Quote Request Form";
    }

    if (empty($name) || empty($email) || empty($phone) || empty($message) || !filter_var($email, FILTER_VALIDATE_EMAIL)) {
        http_response_code(400);
        echo "Please complete all fields with a valid email and try again.";
        exit;
    }
    
    // Basic phone validation (at least 7 digits)
    if (!preg_match('/[0-9]{7,}/', str_replace([' ', '-', '(', ')', '+'], '', $phone))) {
        http_response_code(400);
        echo "Please enter a valid phone number.";
        exit;
    }

    // SMTP Credentials
    $host = 'mail.conxeptrix.com';
    $port = 465;
    $user = 'sales@conxeptrix.com';
    $pass = '1!yl5z^~LjJn,~Dn';

    $recipient = "sales@conxeptrix.com";
    $subject = "New $form_type from $name";

    // Improved HTML Email Formatting
    $email_content = "
    <html>
    <head>
        <style>
            body { font-family: Arial, sans-serif; line-height: 1.6; color: #333; }
            .container { width: 80%; margin: 20px auto; border: 1px solid #ddd; padding: 20px; border-radius: 10px; background-color: #f9f9f9; }
            .header { background: #60d2f0; color: #fff; padding: 10px; text-align: center; border-radius: 10px 10px 0 0; margin: -20px -20px 20px -20px; }
            .field { margin-bottom: 15px; }
            .label { font-weight: bold; color: #555; }
            .value { margin-top: 5px; }
            .footer { margin-top: 20px; font-size: 12px; color: #777; text-align: center; }
        </style>
    </head>
    <body>
        <div class='container'>
            <div class='header'>
                <h2>New Lead Received</h2>
                <p>$form_type</p>
            </div>
            <div class='field'>
                <div class='label'>Name:</div>
                <div class='value'>$name</div>
            </div>
            <div class='field'>
                <div class='label'>Email:</div>
                <div class='value'>$email</div>
            </div>
            <div class='field'>
                <div class='label'>Phone:</div>
                <div class='value'>$phone</div>
            </div>
            <div class='field'>
                <div class='label'>Message:</div>
                <div class='value'>" . nl2br($message) . "</div>
            </div>" . (!empty($serviii) ? "
            <div class='field'>
                <div class='label'>Package:</div>
                <div class='value'>$serviii</div>
            </div>" : "") . "
            <div class='footer'>
                <p>This message was sent from the Conxeptrix website contact form.</p>
            </div>
        </div>
    </body>
    </html>
    ";

    $mailer = new SMTPMailer($host, $port, $user, $pass);
    $result = $mailer->send($recipient, $subject, $email_content, $name, $email, true);

    if ($result === true) {
        $_SESSION['last_submission_time'] = time();
        http_response_code(200);
        header("Location: thankyou.php");
        exit;
    } else {
        http_response_code(500);
        echo "Oops! Something went wrong: " . $result;
    }
} else {
    http_response_code(403);
    echo "There was a problem with your submission, please try again.";
}
?>
