<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Register - Writing Assistant</title>
    <link rel="icon" href="/static/favicon.ico" type="image/x-icon">
    <link rel="stylesheet" href="/static/style.css">
    <style>
        body {
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen, Ubuntu, Cantarell, sans-serif;
        }
        .auth-container {
            display: flex;
            justify-content: center;
            align-items: center;
            min-height: 100vh;
            padding: 20px;
        }
        .auth-box {
            background: white;
            border: 1px solid #e2e8f0;
            border-radius: 8px;
            padding: 40px;
            max-width: 400px;
            width: 100%;
            box-shadow: 0 4px 16px rgba(0, 0, 0, 0.15);
        }
        .auth-header {
            text-align: center;
            margin-bottom: 30px;
        }
        .auth-logo {
            width: 64px;
            height: 64px;
            margin: 0 auto 20px;
            display: block;
        }
        .auth-header h1 {
            margin: 0 0 10px 0;
            font-size: 28px;
            color: #1e293b;
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            background-clip: text;
            -webkit-background-clip: text;
            -webkit-text-fill-color: transparent;
        }
        .auth-header p {
            color: #64748b;
            margin: 0;
        }
        .auth-form {
            display: flex;
            flex-direction: column;
            gap: 20px;
        }
        .form-group {
            display: flex;
            flex-direction: column;
            gap: 8px;
        }
        .form-group label {
            font-weight: 600;
            color: #1e293b;
        }
        .form-group input {
            padding: 12px;
            border: 2px solid #e2e8f0;
            border-radius: 4px;
            font-size: 14px;
            background: white;
            color: #1e293b;
        }
        .form-group input:focus {
            outline: none;
            border-color: #667eea;
            box-shadow: 0 0 0 3px rgba(102, 126, 234, 0.1);
        }
        .form-help {
            font-size: 12px;
            color: #64748b;
        }
        .error-message {
            background: #fee;
            border: 1px solid #fcc;
            color: #c33;
            padding: 12px;
            border-radius: 4px;
            font-size: 14px;
            display: none;
        }
        .success-message {
            background: #efe;
            border: 1px solid #cfc;
            color: #3c3;
            padding: 12px;
            border-radius: 4px;
            font-size: 14px;
            display: none;
        }
        .auth-button {
            padding: 12px;
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            color: white;
            border: none;
            border-radius: 4px;
            font-size: 16px;
            font-weight: 600;
            cursor: pointer;
            transition: all 0.2s;
        }
        .auth-button:hover {
            transform: translateY(-2px);
            box-shadow: 0 4px 12px rgba(102, 126, 234, 0.4);
        }
        .auth-button:disabled {
            background: #ccc;
            cursor: not-allowed;
            transform: none;
        }
        .auth-footer {
            text-align: center;
            margin-top: 20px;
            padding-top: 20px;
            border-top: 1px solid #e2e8f0;
            color: #64748b;
        }
        .auth-footer a {
            color: #667eea;
            text-decoration: none;
            font-weight: 600;
        }
        .auth-footer a:hover {
            text-decoration: underline;
        }
        .password-strength {
            font-size: 12px;
            margin-top: 4px;
        }
        .password-strength.weak {
            color: #c33;
        }
        .password-strength.medium {
            color: #c93;
        }
        .password-strength.strong {
            color: #3c3;
        }
    </style>
</head>
<body>
    <div class="auth-container">
        <div class="auth-box">
            <div class="auth-header">
                <img src="/static/logo.png" alt="Writing Assistant Logo" class="auth-logo">
                <h1>Writing Assistant</h1>
                <p>Create your account</p>
            </div>

            <div id="error-message" class="error-message"></div>
            <div id="success-message" class="success-message"></div>

            <form id="register-form" class="auth-form">
                <div class="form-group">
                    <label for="email">Email</label>
                    <input type="email" id="email" name="email" required placeholder="Enter your email">
                    <span class="form-help">We'll never share your email with anyone else.</span>
                </div>

                <div class="form-group">
                    <label for="password">Password</label>
                    <input type="password" id="password" name="password" required placeholder="Create a password" minlength="8">
                    <span class="form-help">At least 8 characters</span>
                    <div id="password-strength" class="password-strength"></div>
                </div>

                <div class="form-group">
                    <label for="confirm-password">Confirm Password</label>
                    <input type="password" id="confirm-password" name="confirm-password" required placeholder="Confirm your password">
                </div>

                <button type="submit" class="auth-button" id="register-button">
                    Create Account
                </button>
            </form>

            <div class="auth-footer">
                <p>Already have an account? <a href="/login">Login here</a></p>
            </div>
        </div>
    </div>

    <script>
        const registerForm = document.getElementById('register-form');
        const registerButton = document.getElementById('register-button');
        const errorMessage = document.getElementById('error-message');
        const successMessage = document.getElementById('success-message');
        const passwordInput = document.getElementById('password');
        const confirmPasswordInput = document.getElementById('confirm-password');
        const passwordStrength = document.getElementById('password-strength');

        function showError(message) {
            errorMessage.textContent = message;
            errorMessage.style.display = 'block';
            successMessage.style.display = 'none';
        }

        function showSuccess(message) {
            successMessage.textContent = message;
            successMessage.style.display = 'block';
            errorMessage.style.display = 'none';
        }

        // Password strength indicator
        passwordInput.addEventListener('input', () => {
            const password = passwordInput.value;
            let strength = 'weak';
            let text = '';

            if (password.length === 0) {
                passwordStrength.textContent = '';
                passwordStrength.className = 'password-strength';
                return;
            }

            if (password.length >= 8) {
                strength = 'medium';
                text = 'Medium strength';
            }
            if (password.length >= 12 && /[A-Z]/.test(password) && /[0-9]/.test(password)) {
                strength = 'strong';
                text = 'Strong password';
            }
            if (password.length < 8) {
                strength = 'weak';
                text = 'Weak - use at least 8 characters';
            }

            passwordStrength.textContent = text;
            passwordStrength.className = `password-strength ${strength}`;
        });

        registerForm.addEventListener('submit', async (e) => {
            e.preventDefault();

            const email = document.getElementById('email').value;
            const password = passwordInput.value;
            const confirmPassword = confirmPasswordInput.value;

            // Validation
            if (password !== confirmPassword) {
                showError('Passwords do not match');
                return;
            }

            if (password.length < 8) {
                showError('Password must be at least 8 characters long');
                return;
            }

            registerButton.disabled = true;
            registerButton.textContent = 'Creating account...';

            try {
                const response = await fetch('/auth/register', {
                    method: 'POST',
                    headers: {
                        'Content-Type': 'application/json',
                    },
                    body: JSON.stringify({
                        email: email,
                        password: password
                    })
                });

                if (response.ok) {
                    const data = await response.json();
                    showSuccess('Account created successfully! Redirecting to login...');

                    // Redirect to login after 2 seconds
                    setTimeout(() => {
                        window.location.href = '/login';
                    }, 2000);
                } else {
                    const error = await response.json();
                    let errorMsg = 'Registration failed. Please try again.';

                    if (error.detail) {
                        if (typeof error.detail === 'string') {
                            errorMsg = error.detail;
                        } else if (error.detail.code === 'REGISTER_USER_ALREADY_EXISTS') {
                            errorMsg = 'An account with this email already exists.';
                        }
                    }

                    showError(errorMsg);
                    registerButton.disabled = false;
                    registerButton.textContent = 'Create Account';
                }
            } catch (error) {
                showError('An error occurred. Please try again.');
                console.error('Registration error:', error);
                registerButton.disabled = false;
                registerButton.textContent = 'Create Account';
            }
        });

        // Check if already logged in
        if (localStorage.getItem('access_token')) {
            window.location.href = '/';
        }
    </script>
</body>
</html>
