<?php
require_once 'includes/config.php';
require_once 'includes/email_config.php';

echo "<h2>Email Configuration Test</h2>";

// Display current configuration
echo "<h3>Current Email Configuration:</h3>";
echo "<ul>";
echo "<li><strong>SMTP_HOST:</strong> " . SMTP_HOST . "</li>";
echo "<li><strong>SMTP_PORT:</strong> " . SMTP_PORT . "</li>";
echo "<li><strong>SMTP_USERNAME:</strong> " . SMTP_USERNAME . "</li>";
echo "<li><strong>SMTP_ENCRYPTION:</strong> " . SMTP_ENCRYPTION . "</li>";
echo "<li><strong>SITE_NAME:</strong> " . SITE_NAME . "</li>";
echo "<li><strong>SUPPORT_EMAIL:</strong> " . SUPPORT_EMAIL . "</li>";
echo "<li><strong>NO_REPLY_EMAIL:</strong> " . NO_REPLY_EMAIL . "</li>";
echo "</ul>";

// Check PHP configuration
echo "<h3>PHP Configuration:</h3>";
echo "<ul>";
echo "<li><strong>allow_url_fopen:</strong> " . (ini_get('allow_url_fopen') ? 'Enabled' : 'Disabled') . "</li>";
echo "<li><strong>PHP version:</strong> " . phpversion() . "</li>";
echo "</ul>";

// Check if required files exist
echo "<h3>File System Check:</h3>";
$files = [
    'includes/email_config.php' => file_exists('includes/email_config.php'),
    'includes/email_functions.php' => file_exists('includes/email_functions.php'),
    'includes/PHPMailer/src/PHPMailer.php' => file_exists('includes/PHPMailer/src/PHPMailer.php'),
    'includes/PHPMailer/src/SMTP.php' => file_exists('includes/PHPMailer/src/SMTP.php'),
    'includes/PHPMailer/src/Exception.php' => file_exists('includes/PHPMailer/src/Exception.php')
];

echo "<ul>";
foreach ($files as $file => $exists) {
    echo "<li><strong>" . $file . ":</strong> " . ($exists ? '<span style="color: green;">Found</span>' : '<span style="color: red;">Not Found</span>') . "</li>";
}
echo "</ul>";

// Test email function
echo "<h3>Send Test Email:</h3>";
echo "<form method='post'>";
echo "<p><label for='email'>Send test email to:</label><br>";
echo "<input type='email' id='email' name='email' value='" . (isset($_POST['email']) ? htmlspecialchars($_POST['email']) : SMTP_USERNAME) . "' style='width: 300px;'></p>";
echo "<p><button type='submit'>Send Test Email</button></p>";
echo "</form>";

if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['email'])) {
    require_once 'includes/email_functions.php';
    
    $to = $_POST['email'];
    $subject = 'StoreWeb Email Configuration Test';
    $message = '
    <html>
    <body>
        <h2>Email Configuration Test</h2>
        <p>This is a test email from your StoreWeb application.</p>
        <p>If you receive this email, your email configuration is working correctly.</p>
        <p><strong>Site:</strong> ' . SITE_NAME . '</p>
        <p><strong>Time:</strong> ' . date('Y-m-d H:i:s') . '</p>
    </body>
    </html>
    ';
    
    echo "<h3>Sending Test Email...</h3>";
    
    if (sendEmail($to, $subject, $message)) {
        echo "<p style='color: green; font-weight: bold;'>Email sent successfully!</p>";
        echo "<p>Please check your inbox (and spam folder) for the test email.</p>";
    } else {
        echo "<p style='color: red; font-weight: bold;'>Failed to send email.</p>";
        echo "<h4>Troubleshooting Steps:</h4>";
        echo "<ol>";
        echo "<li>Install PHPMailer by visiting <a href='install_phpmailer.php'>install_phpmailer.php</a></li>";
        echo "<li>Check your Gmail account security settings</li>";
        echo "<li>Ensure you're using an App Password if 2-Factor Authentication is enabled</li>";
        echo "<li>Verify SMTP settings in <code>includes/email_config.php</code></li>";
        echo "</ol>";
    }
}

echo "<p><a href='index.php'>Back to Home</a></p>";
?>