<?php
// Test page for email configuration (admin only)
require_login();
require_admin();
require_once 
__DIR__ . '/../includes/email_phpmailer_manual.php';

$testResult = null;
$testEmail = '';

if (
$_SERVER['REQUEST_METHOD'] === 'POST') {
    
$testEmail = trim($_POST['test_email'] ?? '');
    if (
$testEmail) {
        
$testResult = test_email_configuration($testEmail);
    } else {
        
$testResult = ['success' => false, 'message' => 'Please enter a valid email address'];
    }
}
?>

<div class="page-email-test" style="max-width: 600px; margin: 0 auto;">
    <div class="page-header">
        <h1 class="page-title">๐Ÿ“ง Email Configuration Test</h1>
        <p class="page-subtitle">Test your PHPMailer SMTP configuration</p>
    </div>
    
    <?php if ($testResult): ?>
        <div class="alert <?= $testResult['success'] ? 'alert-success' : 'alert-error' ?>" style="margin-bottom: 2rem;">
            <strong><?= $testResult['success'] ? 'โœ… Success!' : 'โŒ Error!' ?></strong><br>
            <?= htmlspecialchars($testResult['message']) ?>
        </div>
    <?php endif; ?>
    
    <div class="card" style="margin-bottom: 2rem;">
        <div class="card-body">
            <h3>๐Ÿ“‹ Current Configuration</h3>
            <table class="table">
                <tr>
                    <td><strong>SMTP Host:</strong></td>
                    <td><?= htmlspecialchars(SMTP_HOST) ?></td>
                </tr>
                <tr>
                    <td><strong>SMTP Port:</strong></td>
                    <td><?= htmlspecialchars(SMTP_PORT) ?></td>
                </tr>
                <tr>
                    <td><strong>SMTP Security:</strong></td>
                    <td><?= htmlspecialchars(SMTP_SECURE) ?></td>
                </tr>
                <tr>
                    <td><strong>Username:</strong></td>
                    <td><?= htmlspecialchars(SMTP_USERNAME) ?></td>
                </tr>
                <tr>
                    <td><strong>From Email:</strong></td>
                    <td><?= htmlspecialchars(FROM_EMAIL) ?></td>
                </tr>
                <tr>
                    <td><strong>Email Notifications:</strong></td>
                    <td><?= ENABLE_EMAIL_NOTIFICATIONS ? 'โœ… Enabled' : 'โŒ Disabled' ?></td>
                </tr>
                <tr>
                    <td><strong>Debug Mode:</strong></td>
                    <td><?= EMAIL_DEBUG ? '๐Ÿ” On' : '๐Ÿ”‡ Off' ?></td>
                </tr>
            </table>
        </div>
    </div>
    
    <div class="card">
        <div class="card-body">
            <h3>๐Ÿงช Send Test Email</h3>
            <form method="post" style="margin-top: 1rem;">
                <div class="form-group">
                    <label class="form-label" for="test_email">Test Email Address:</label>
                    <input type="email" id="test_email" name="test_email" class="form-input" 
                           value="<?= htmlspecialchars($testEmail ?: SMTP_USERNAME) ?>" required
                           placeholder="Enter email to test">
                    <small style="color: #64748b;">We'll send a test email to this address</small>
                </div>
                <button type="submit" class="btn btn-primary">๐Ÿ“ค Send Test Email</button>
            </form>
        </div>
    </div>
    
    <div class="card" style="margin-top: 2rem; background: #fffbeb; border: 1px solid #fed7aa;">
        <div class="card-body">
            <h3 style="color: #d97706;">โš ๏ธ Gmail Setup Required</h3>
            <p><strong>Your password won't work with Gmail!</strong> You need to:</p>
            <ol>
                <li>Enable 2-Factor Authentication on your Gmail account</li>
                <li>Generate an "App Password" for this application</li>
                <li>Use the App Password instead of your regular password</li>
            </ol>
            <p><strong>Steps:</strong></p>
            <ol>
                <li>Go to <a href="https://myaccount.google.com/security" target="_blank">Google Account Security</a></li>
                <li>Enable "2-Step Verification" if not already enabled</li>
                <li>Go to "App passwords" section</li>
                <li>Generate a new app password for "Mail"</li>
                <li>Copy that 16-character password</li>
                <li>Replace your current password in email_config.php</li>
            </ol>
        </div>
    </div>
</div>