<?php
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;

require 'auth/mailer/src/Exception.php';
require 'auth/mailer/src/PHPMailer.php';
require 'auth/mailer/src/SMTP.php';

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $name    = $_POST['name'] ?? '';
    $email   = $_POST['email'] ?? '';
    $message = $_POST['message'] ?? '';

    if (empty($name) || empty($email) || empty($message)) {
        echo "missing";
        exit;
    }

    $mail = new PHPMailer(true);

    try {
        // SMTP config
        $mail->isSMTP();
        $mail->Host       = 'mail.gobolchain.info';
        $mail->SMTPAuth   = true;
        $mail->Username   = 'team@gobolchain.info';
        $mail->Password   = '@Jibala1997'; // ← Be sure this is correct
        $mail->SMTPSecure = 'ssl';
        $mail->Port       = 465;

        // Email content
        $mail->setFrom('team@gobolchain.info', 'Gobolchain Support');
        $mail->addAddress('team@gobolchain.info'); // Send to self

        $mail->isHTML(true);
        $mail->Subject = "New Support Message from $name";
        $mail->Body    = "
            <h2>Support Message</h2>
            <p><strong>Name:</strong> " . htmlspecialchars($name) . "</p>
            <p><strong>Email:</strong> " . htmlspecialchars($email) . "</p>
            <p><strong>Message:</strong><br>" . nl2br(htmlspecialchars($message)) . "</p>
        ";

        $mail->send();
        echo "success";
        exit; // ✅ stop here if successful
    } catch (Exception $e) {
        echo "Mailer Error: " . $mail->ErrorInfo;
        error_log("Mailer Error: " . $mail->ErrorInfo);
        exit; // ✅ stop here if failed
    }
}

echo "invalid"; // This runs only if not a POST request