<!DOCTYPE html>
<html lang="fr">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Connexion - Ohm Stream Downloader</title>
    <link rel="stylesheet" href="/static/css/style.css">
</head>
<body>
    <div class="auth-container">
        <h1 class="auth-title">🎬 Ohm Stream</h1>

        <div class="auth-tabs">
            <div class="auth-tab active" data-tab="login">Connexion</div>
            <div class="auth-tab" data-tab="register">Inscription</div>
        </div>

        <div class="auth-error" id="authError" aria-live="polite"></div>
        <div class="auth-success" id="authSuccess" aria-live="polite"></div>

        <!-- Login Form -->
        <form class="auth-form active" id="loginForm">
            <div class="form-group">
                <label for="loginUsername">Nom d'utilisateur</label>
                <input
                    type="text"
                    id="loginUsername"
                    placeholder="Entrez votre nom d'utilisateur"
                    required
                    aria-required="true"
                    aria-describedby="loginUsernameHelp"
                >
                <span id="loginUsernameHelp" style="display: none;">Champ obligatoire</span>
            </div>
            <div class="form-group">
                <label for="loginPassword">Mot de passe</label>
                <input
                    type="password"
                    id="loginPassword"
                    placeholder="Entrez votre mot de passe"
                    required
                    aria-required="true"
                >
            </div>
            <button type="submit" id="loginSubmit" class="btn btn-primary btn-block">Se connecter</button>
        </form>

        <!-- Register Form -->
        <form class="auth-form" id="registerForm">
            <div class="form-group">
                <label for="registerUsername">Nom d'utilisateur</label>
                <input
                    type="text"
                    id="registerUsername"
                    placeholder="Choisissez un nom d'utilisateur"
                    minlength="3"
                    required
                    aria-required="true"
                >
            </div>
            <div class="form-group">
                <label for="registerEmail">Email (optionnel)</label>
                <input
                    type="email"
                    id="registerEmail"
                    placeholder="votre@email.com"
                >
            </div>
            <div class="form-group">
                <label for="registerFullName">Nom complet (optionnel)</label>
                <input
                    type="text"
                    id="registerFullName"
                    placeholder="Votre nom complet"
                >
            </div>
            <div class="form-group">
                <label for="registerPassword">Mot de passe</label>
                <input
                    type="password"
                    id="registerPassword"
                    placeholder="Au moins 6 caractères"
                    minlength="6"
                    required
                    aria-required="true"
                >
            </div>
            <div class="form-group">
                <label for="registerPasswordConfirm">Confirmer le mot de passe</label>
                <input
                    type="password"
                    id="registerPasswordConfirm"
                    placeholder="Confirmez votre mot de passe"
                    minlength="6"
                    required
                    aria-required="true"
                >
            </div>
            <button type="submit" id="registerSubmit" class="btn btn-primary btn-block">S'inscrire</button>
        </form>

        <div style="text-align: center; margin-top: 25px;">
            <a href="/web" class="btn btn-secondary btn-small">← Retour à l'accueil</a>
        </div>
    </div>

    <!-- Load auth modules in order -->
    <script src="/static/js/auth-utils.js"></script>
    <script src="/static/js/auth-api.js"></script>
    <script src="/static/js/auth-ui.js"></script>
    <script>
        // Expose setToken from auth.js if available
        if (typeof window.setToken === 'undefined') {
            window.setToken = function(token) {
                localStorage.setItem('auth_token', token);
                document.cookie = 'auth_token=' + token + ';path=/;SameSite=Strict';
            };
        }

        // Attach event listeners after all scripts are loaded
        document.addEventListener('DOMContentLoaded', function() {
            const loginForm = document.getElementById('loginForm');
            const registerForm = document.getElementById('registerForm');

            if (loginForm && window.authUi && window.authUi.handleLogin) {
                loginForm.addEventListener('submit', window.authUi.handleLogin);
            }

            if (registerForm && window.authUi && window.authUi.handleRegister) {
                registerForm.addEventListener('submit', window.authUi.handleRegister);
            }

            // Attach tab click handlers
            const tabs = document.querySelectorAll('.auth-tab');
            tabs.forEach(tab => {
                tab.addEventListener('click', function() {
                    if (window.authUi && window.authUi.switchTab) {
                        window.authUi.switchTab(this.dataset.tab);
                    }
                });
            });
        });
    </script>
</body>
</html>
