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

echo "<h2>Email Troubleshooter</h2>";

// Step 1: Check configuration
echo "<h3>Step 1: Configuration Check</h3>";
$config_issues = [];

if (empty(SMTP_HOST)) {
    $config_issues[] = "SMTP_HOST is not set";
}

if (empty(SMTP_PORT)) {
    $config_issues[] = "SMTP_PORT is not set";
}

if (empty(SMTP_USERNAME)) {
    $config_issues[] = "SMTP_USERNAME is not set";
}

if (empty(SMTP_PASSWORD)) {
    $config_issues[] = "SMTP_PASSWORD is not set";
}

if (empty(SMTP_ENCRYPTION)) {
    $config_issues[] = "SMTP_ENCRYPTION is not set";
}

if (empty(SITE_NAME)) {
    $config_issues[] = "SITE_NAME is not set";
}

if (empty(SUPPORT_EMAIL)) {
    $config_issues[] = "SUPPORT_EMAIL is not set";
}

if (empty(NO_REPLY_EMAIL)) {
    $config_issues[] = "NO_REPLY_EMAIL is not set";
}

if (empty($config_issues)) {
    echo "<p style='color: green;'>✓ All configuration values are set</p>";
} else {
    echo "<p style='color: red;'>✗ Configuration issues found:</p>";
    echo "<ul>";
    foreach ($config_issues as $issue) {
        echo "<li>$issue</li>";
    }
    echo "</ul>";
}

// Step 2: Check file existence
echo "<h3>Step 2: File System Check</h3>";
$files = [
    'includes/config.php' => file_exists('includes/config.php'),
    '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')
];

$missing_files = [];
foreach ($files as $file => $exists) {
    if ($exists) {
        echo "<p style='color: green;'>✓ $file - Found</p>";
    } else {
        echo "<p style='color: red;'>✗ $file - Not Found</p>";
        $missing_files[] = $file;
    }
}

// Step 3: Check PHP settings
echo "<h3>Step 3: PHP Settings Check</h3>";
$php_settings = [
    'allow_url_fopen' => ini_get('allow_url_fopen'),
    'openssl' => extension_loaded('openssl'),
    'sockets' => extension_loaded('sockets'),
    'curl' => extension_loaded('curl')
];

foreach ($php_settings as $setting => $value) {
    if ($value) {
        echo "<p style='color: green;'>✓ $setting - Enabled</p>";
    } else {
        echo "<p style='color: red;'>✗ $setting - Disabled</p>";
    }
}

// Step 4: Test email sending
echo "<h3>Step 4: Email Sending Test</h3>";

if (empty($missing_files)) {
    require_once 'includes/email_functions.php';
    
    $to = SMTP_USERNAME; // Send to the same account for testing
    $subject = 'StoreWeb Email Test - ' . date('Y-m-d H:i:s');
    $message = '
    <html>
    <body>
        <h2>StoreWeb Email Test</h2>
        <p>This is a test email to verify that 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>
        <p><strong>Server:</strong> ' . $_SERVER['SERVER_NAME'] . '</p>
    </body>
    </html>
    ';
    
    echo "<p>Sending test email to: <strong>$to</strong></p>";
    
    // Log the attempt
    error_log("Email troubleshooter: Attempting to send test email to $to");
    
    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>";
        
        // Provide specific troubleshooting steps
        echo "<h4>Troubleshooting Steps:</h4>";
        echo "<ol>";
        if (in_array('includes/PHPMailer/src/PHPMailer.php', $missing_files)) {
            echo "<li><strong>Install PHPMailer:</strong> Visit <a href='install_phpmailer.php'>install_phpmailer.php</a> or <a href='create_phpmailer_files.php'>create_phpmailer_files.php</a></li>";
        }
        echo "<li><strong>Check Gmail Settings:</strong>";
        echo "<ul>";
        echo "<li>Enable 2-Factor Authentication on your Gmail account</li>";
        echo "<li>Generate an App Password for use instead of your regular password</li>";
        echo "<li>Ensure 'Less secure app access' is enabled (if not using App Passwords)</li>";
        echo "</ul></li>";
        echo "<li><strong>Verify SMTP Configuration:</strong>";
        echo "<ul>";
        echo "<li>Check that SMTP settings in <code>includes/email_config.php</code> are correct</li>";
        echo "<li>Ensure your Gmail credentials are correct</li>";
        echo "</ul></li>";
        echo "<li><strong>Check Server Configuration:</strong>";
        echo "<ul>";
        echo "<li>Ensure sendmail is properly configured if using XAMPP</li>";
        echo "<li>Check PHP error logs for more details</li>";
        echo "</ul></li>";
        echo "</ol>";
    }
} else {
    echo "<p style='color: orange;'>Skipping email test due to missing files.</p>";
    echo "<p>Please install the missing files first:</p>";
    echo "<ul>";
    foreach ($missing_files as $file) {
        echo "<li>$file</li>";
    }
    echo "</ul>";
    echo "<p>You can:</p>";
    echo "<ul>";
    echo "<li>Visit <a href='install_phpmailer.php'>install_phpmailer.php</a> for automatic installation</li>";
    echo "<li>Visit <a href='create_phpmailer_files.php'>create_phpmailer_files.php</a> for minimal files</li>";
    echo "<li>Follow the manual installation guide in <a href='MANUAL_PHPMAILER_INSTALL.md'>MANUAL_PHPMAILER_INSTALL.md</a></li>";
    echo "</ul>";
}

// Step 5: Recommendations
echo "<h3>Step 5: Recommendations</h3>";
echo "<ul>";
echo "<li><strong>For Development:</strong> Consider using Mailtrap (https://mailtrap.io) for email testing</li>";
echo "<li><strong>For Production:</strong> Use a proper email service like SendGrid or Amazon SES</li>";
echo "<li><strong>Security:</strong> Always use App Passwords with Gmail 2-Factor Authentication</li>";
echo "<li><strong>Monitoring:</strong> Check PHP error logs regularly for email delivery issues</li>";
echo "</ul>";

echo "<p><a href='index.php'>Back to Home</a> | <a href='email_config_test.php'>Email Configuration Test</a></p>";
?>