<!DOCTYPE html>
<html lang="fr">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no, viewport-fit=cover">
    <title>Agora — Connexion</title>
    
    <link rel="manifest" href="/manifest.json">
    <meta name="theme-color" content="#1a1a1a">

    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="apple-mobile-web-app-status-bar-style" content="black-translucent">
    <meta name="apple-mobile-web-app-title" content="Agora">
    <link rel="apple-touch-icon" href="/images/icone-192.png">

    <link href="https://fonts.googleapis.com/css2?family=Inter:wght@400;500;600;700&display=swap" rel="stylesheet">
    <link rel="stylesheet" href="/style.css">
    <script src="/lucide.min.js"></script>
    <script src="/app.auth.js"></script>
    <style>
        .error-msg {
            color: var(--primary);
            font-size: 0.95rem;
            margin-top: 15px;
            font-weight: 600;
        }
    </style>
    <link rel="icon" type="image/svg+xml" href="/logo_svg.svg">
</head>
<body>

    <div id="login-overlay" class="overlay active">
        <div class="login-box">
            <div class="logo-large">
                <img src="/images/logo_svg.svg" alt="Logo" class="icon-xl">
                <h1>Agora</h1>
            </div>
            <form id="login-form">
                <input type="text" id="login-username" placeholder="Nom d'utilisateur" required autofocus autocomplete="username">
                <div class="password-container">
                    <input type="password" id="login-password" placeholder="Mot de passe" required autocomplete="current-password">
                    <button type="button" class="toggle-password" tabindex="-1">
                        <i data-lucide="eye" class="eye-icon"></i>
                    </button>
                </div>
                <button type="submit">Connexion</button>
            </form>
            <div id="login-error" class="error-msg hidden"></div>
        </div>
    </div>

    <script>
        if (typeof lucide !== 'undefined') lucide.createIcons();
        if (window.AuthHelpers) AuthHelpers.initPasswordToggles();

        document.getElementById('login-form').addEventListener('submit', async (e) => {
            e.preventDefault();
            const username = document.getElementById('login-username').value.trim();
            const password = document.getElementById('login-password').value;
            const errorDiv = document.getElementById('login-error');
            
            errorDiv.classList.add('hidden');
            errorDiv.textContent = '';

            try {
                const res = await fetch('/api/login', {
                    method: 'POST',
                    headers: { 'Content-Type': 'application/json' },
                    body: JSON.stringify({ username, password })
                });
                const data = await res.json();

                if (data.success) {
                    // Redirection vers la page par défaut
                    const defaultPage = document.cookie.split('; ').find(c => c.startsWith('defaultPage='));
                    const target = defaultPage ? defaultPage.split('=')[1] : '/trending';
                    window.location.href = target;
                } else {
                    errorDiv.textContent = data.error || 'Identifiants invalides.';
                    errorDiv.classList.remove('hidden');
                }
            } catch (err) {
                errorDiv.textContent = 'Erreur de connexion au serveur.';
                errorDiv.classList.remove('hidden');
            }
        });
    </script>
</body>
</html>
