<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Login - Masina-Dock</title>
    <link rel="stylesheet" href="/static/css/style.css">
    <style>
        .auth-container {
            display: flex;
            justify-content: center;
            align-items: center;
            min-height: 100vh;
            padding: 20px;
        }
        .auth-card {
            background: var(--card-bg);
            border-radius: 12px;
            padding: 40px;
            max-width: 450px;
            width: 100%;
            box-shadow: 0 4px 6px rgba(0, 0, 0, 0.3);
        }
        .auth-card h1 {
            text-align: center;
            margin-bottom: 10px;
            color: var(--text-primary);
        }
        .auth-card h2 {
            text-align: center;
            margin-bottom: 30px;
            color: var(--text-secondary);
            font-weight: 400;
        }
    </style>
</head>
<body>
    <div class="auth-container">
        <div class="auth-card">
            <h1>Masina-Dock</h1>
            <h2>Login</h2>
            
            <form id="login-form">
                <div class="form-group">
                    <label for="username">Username</label>
                    <input type="text" id="username" name="username" required autofocus>
                </div>
                <div class="form-group">
                    <label for="password">Password</label>
                    <input type="password" id="password" name="password" required>
                </div>
                <button type="submit" class="btn btn-success" style="width: 100%;">Login</button>
            </form>
            
            <p style="text-align: center; margin-top: 20px;">
                Don't have an account? <a href="/register" style="color: var(--primary);">Register</a>
            </p>
        </div>
    </div>
    
    <div id="custom-alert-modal" class="modal">
        <div class="modal-content" style="max-width: 400px; text-align: center;">
            <p id="alert-message" style="margin: 20px 0; font-size: 16px;"></p>
            <button class="btn btn-success" onclick="closeCustomAlert()" style="width: 100px;">OK</button>
        </div>
    </div>
    
    <div id="2fa-modal" class="modal">
        <div class="modal-content" style="max-width: 400px;">
            <h2>Two-Factor Authentication</h2>
            <p>Enter the 6-digit code from your authenticator app:</p>
            <form id="2fa-form">
                <div class="form-group">
                    <label for="2fa-code">Authentication Code</label>
                    <input type="text" id="2fa-code" name="code" required maxlength="6" placeholder="000000" style="text-align: center; font-size: 24px; letter-spacing: 5px;">
                </div>
                <button type="submit" class="btn btn-success" style="width: 100%;">Verify</button>
            </form>
            <p style="text-align: center; margin-top: 15px; color: var(--text-secondary); font-size: 14px;">
                Lost your device? Use a backup code instead.
            </p>
        </div>
    </div>
    
    <script src="/static/js/app.js"></script>
    <script>
        let tempUserId = null;
        
        function showCustomAlert(message) {
            document.getElementById('alert-message').textContent = message;
            document.getElementById('custom-alert-modal').classList.add('active');
        }
        
        function closeCustomAlert() {
            document.getElementById('custom-alert-modal').classList.remove('active');
        }
        
        function showModal(modalId) {
            document.getElementById(modalId).classList.add('active');
        }
        
        function closeModal(modalId) {
            document.getElementById(modalId).classList.remove('active');
        }
        
        document.getElementById('login-form').addEventListener('submit', async (e) => {
            e.preventDefault();
            
            const username = document.getElementById('username').value;
            const password = document.getElementById('password').value;
            
            try {
                const response = await fetch('/api/auth/login', {
                    method: 'POST',
                    credentials: 'include',
                    headers: {
                        'Content-Type': 'application/json',
                        'X-Requested-With': 'XMLHttpRequest'
                    },
                    body: JSON.stringify({ username, password })
                });
                
                const data = await response.json();
                
                if (response.ok) {
                    if (data.requires_2fa) {
                        tempUserId = data.user_id;
                        showModal('2fa-modal');
                    } else {
                        localStorage.setItem('userSettings', JSON.stringify(data.user));
                        
                        if (data.user.must_change_credentials) {
                            window.location.href = '/first-login';
                        } else {
                            window.location.href = '/dashboard';
                        }
                    }
                } else {
                    showCustomAlert(data.error || 'Login failed');
                }
            } catch (error) {
                showCustomAlert('Login failed: ' + error.message);
            }
        });
        
        document.getElementById('2fa-form').addEventListener('submit', async (e) => {
            e.preventDefault();
            
            const code = document.getElementById('2fa-code').value;
            
            try {
                const response = await fetch('/api/auth/verify-2fa', {
                    method: 'POST',
                    credentials: 'include',
                    headers: {
                        'Content-Type': 'application/json',
                        'X-Requested-With': 'XMLHttpRequest'
                    },
                    body: JSON.stringify({ 
                        user_id: tempUserId,
                        code: code 
                    })
                });
                
                const data = await response.json();
                
                if (response.ok) {
                    localStorage.setItem('userSettings', JSON.stringify(data.user));
                    
                    if (data.user.must_change_credentials) {
                        window.location.href = '/first-login';
                    } else {
                        window.location.href = '/dashboard';
                    }
                } else {
                    showCustomAlert(data.error || 'Invalid 2FA code');
                    document.getElementById('2fa-code').value = '';
                }
            } catch (error) {
                showCustomAlert('Verification failed: ' + error.message);
            }
        });
    </script>
</body>
</html>
