<?php
// ==================== EMAIL TEST SCRIPT ====================
ini_set('display_errors', 1);
error_reporting(E_ALL);

require_once __DIR__ . '/config/db.php';           // your PDO + constants
require_once __DIR__ . '/config/reviewer_mailer.php';

echo "<h2>Gmail SMTP Test</h2>";
echo "<pre>";

// Check if PHPMailer is loaded
if (!class_exists('PHPMailer\PHPMailer\PHPMailer')) {
    die("❌ PHPMailer not found! Check reviewer_mailer.php loading paths.");
}

echo "✅ PHPMailer loaded\n";
echo "SMTP User : " . GMAIL_SMTP_USER . "\n";
echo "SMTP Pass length : " . strlen(GMAIL_SMTP_PASS) . " characters\n\n";

// ==================== TEST FUNCTION ====================
function testSendEmail($to_email, $to_name = "Test Receiver") {
    global $pdo; // not needed but ok

    $subject = "Test Email from RSIS Reviewer System - " . date('Y-m-d H:i');
    
    $body = "
    <h2>This is a test email</h2>
    <p>If you receive this, the mailer is working correctly.</p>
    <p>Sent at: " . date('Y-m-d H:i:s') . "</p>
    <hr>
    <small>Debug test from test_email.php</small>";

    echo "Attempting to send to: $to_email\n";

    $result = _sendHtmlMailSMTP($to_email, $to_name, $subject, $body);

    if ($result) {
        echo "✅ SUCCESS! Email should have been sent.\n";
    } else {
        echo "❌ FAILED to send email.\n";
        echo "Check the PHP error log for the exact error message.\n";
    }
}

// ==================== RUN THE TEST ====================

// Change this to YOUR email address
$test_email = 'ayushigautam2692@gmail.com';   // ←←← CHANGE THIS

if (empty($test_email) || !filter_var($test_email, FILTER_VALIDATE_EMAIL)) {
    die("Please set a valid test email address in the script.");
}

testSendEmail($test_email);

echo "</pre>";
?>