<!DOCTYPE html>
<html lang="fr" data-theme="ohmstream">
<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">
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.5.1/css/all.min.css">
</head>
<body>
    <div class="min-h-screen flex items-center justify-center bg-base-100">
        <div class="card w-96 bg-base-200 shadow-2xl">
            <div class="card-body">
                <!-- Title -->
                <h1 class="text-2xl font-bold text-center text-primary">
                    <i class="fa-solid fa-film"></i> Ohm Stream
                </h1>

                <!-- Tab Toggle -->
                <div class="tabs tabs-boxed bg-base-300 mb-4" role="tablist">
                    <button class="tab tab-active auth-tab" role="tab" data-tab="login">Connexion</button>
                    <button class="tab auth-tab" role="tab" data-tab="register">Inscription</button>
                </div>

                <!-- Error / Success Alerts -->
                <div id="authError" class="alert alert-error hidden mb-2" role="alert" aria-live="polite">
                    <i class="fa-solid fa-circle-exclamation"></i>
                    <span></span>
                </div>
                <div id="authSuccess" class="alert alert-success hidden mb-2" role="status" aria-live="polite">
                    <i class="fa-solid fa-circle-check"></i>
                    <span></span>
                </div>

                <!-- Login Form -->
                <form id="loginForm">
                    <div class="form-control mb-3">
                        <label class="label" for="loginUsername">
                            <span class="label-text">Nom d'utilisateur</span>
                        </label>
                        <input
                            type="text"
                            id="loginUsername"
                            placeholder="Entrez votre nom d'utilisateur"
                            class="input input-bordered w-full"
                            required
                            aria-required="true"
                            aria-describedby="loginUsernameHelp"
                        >
                        <label class="label hidden" id="loginUsernameHelp">
                            <span class="label-text-alt text-error">Champ obligatoire</span>
                        </label>
                    </div>
                    <div class="form-control mb-3">
                        <label class="label" for="loginPassword">
                            <span class="label-text">Mot de passe</span>
                        </label>
                        <input
                            type="password"
                            id="loginPassword"
                            placeholder="Entrez votre mot de passe"
                            class="input input-bordered w-full"
                            required
                            aria-required="true"
                        >
                    </div>
                    <button type="submit" id="loginSubmit" class="btn btn-primary w-full">Se connecter</button>
                </form>

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

                <!-- Back Link -->
                <div class="text-center mt-5">
                    <a href="/web" class="btn btn-ghost btn-sm">
                        <i class="fa-solid fa-arrow-left"></i> Retour à l'accueil
                    </a>
                </div>
            </div>
        </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>
        // Patch displayError / displaySuccess to work with DaisyUI alerts
        (function () {
            const origDisplayError = window.displayError;
            const origDisplaySuccess = window.displaySuccess;

            window.displayError = function (id, message) {
                const el = document.getElementById(id);
                if (!el) return;
                el.classList.remove('hidden');
                el.querySelector('span').textContent = message || '';
            };

            window.displaySuccess = function (id, message) {
                const el = document.getElementById(id);
                if (!el) return;
                el.classList.remove('hidden');
                el.querySelector('span').textContent = message || '';
            };
        })();

        // 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>
