<?php
require_once 'config/mail.php';
require_once 'phpmailer/src/PHPMailer.php';
require_once 'phpmailer/src/SMTP.php';
require_once 'phpmailer/src/Exception.php';

use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;

$result = '';
$error = '';
$debug_log = '';

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $to = trim($_POST['to_email'] ?? '');
    
    if (empty($to)) {
        $error = "Please enter an email address.";
    } else {
        $mail = new PHPMailer(true);
        try {
            // Enable FULL debug output
            $mail->SMTPDebug = 2;
            $mail->Debugoutput = function($str, $level) use (&$debug_log) {
                $debug_log .= htmlspecialchars($str) . "<br>\n";
            };
            
            $mail->isSMTP();
            $mail->Host       = SMTP_HOST;
            $mail->SMTPAuth   = true;
            $mail->Username   = SMTP_USER;
            $mail->Password   = SMTP_PASS;
            $mail->SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS;
            $mail->Port       = SMTP_PORT;
            
            $mail->setFrom(SMTP_FROM_EMAIL, SMTP_FROM_NAME);
            $mail->addAddress($to);
            $mail->Subject = 'SMTP Test with Debug';
            $mail->Body    = "This is a test email.<br><br>Time: " . date('Y-m-d H:i:s');
            
            $mail->send();
            $result = "✅ Email sent successfully to $to!";
        } catch (Exception $e) {
            $error = "❌ Failed: " . $mail->ErrorInfo;
        }
    }
}
?>
<!DOCTYPE html>
<html>
<head><title>SMTP Debug Test</title><script src="https://cdn.tailwindcss.com"></script></head>
<body class="bg-gray-100 p-8">
<div class="max-w-2xl mx-auto bg-white rounded-xl shadow-md p-6">
    <h1 class="text-2xl font-bold mb-2">🔍 SMTP Debug Test</h1>
    
    <?php if ($result): ?>
        <div class="bg-green-100 border border-green-400 text-green-700 px-4 py-3 rounded mb-4"><?php echo $result; ?></div>
    <?php endif; ?>
    <?php if ($error): ?>
        <div class="bg-red-100 border border-red-400 text-red-700 px-4 py-3 rounded mb-4"><?php echo $error; ?></div>
    <?php endif; ?>
    
    <form method="POST">
        <div>
            <label class="block font-medium mb-1">Your Email:</label>
            <input type="email" name="to_email" required class="w-full p-2 border rounded">
        </div>
        <button type="submit" class="w-full bg-blue-600 text-white py-2 rounded mt-4 hover:bg-blue-700">Send with Debug</button>
    </form>
    
    <?php if ($debug_log): ?>
        <hr class="my-4">
        <div class="bg-gray-900 text-green-400 p-3 rounded text-xs overflow-auto max-h-96 font-mono">
            <strong class="text-white">SMTP Debug Log:</strong><br><br>
            <?php echo $debug_log; ?>
        </div>
    <?php endif; ?>
    
    <div class="mt-4 p-3 bg-yellow-50 border border-yellow-200 rounded text-sm">
        <strong>Current Settings:</strong><br>
        Host: <code><?php echo SMTP_HOST; ?></code><br>
        Port: <code><?php echo SMTP_PORT; ?></code><br>
        Username: <code><?php echo SMTP_USER; ?></code><br>
        From: <code><?php echo SMTP_FROM_EMAIL; ?></code>
    </div>
</div>
</body>
</html>