<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Register</title>
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css">
    <style>
        body {
            background-color: #f8f9fa;
        }

        .container {
            max-width: 400px;
            margin-top: 50px;
        }
    </style>
</head>

<body>
    <div class="container">
        <h2 class="text-center mb-4">Register</h2>
        <form id="registerForm">
            <div class="mb-3">
                <label for="username" class="form-label">Username</label>
                <input type="text" class="form-control" id="username" name="username" required>
            </div>
            <div class="mb-3">
                <label for="email" class="form-label">Email</label>
                <input type="email" class="form-control" id="email" name="email" required>
            </div>
            <div class="mb-3">
                <label for="password" class="form-label">Password</label>
                <input type="password" class="form-control" id="password" name="password" required>
            </div>
            <button type="submit" class="btn btn-primary w-100">Register</button>
            <div id="message" class="mt-3 text-center"></div>
        </form>
        <p class="mt-3 text-center">Already have an account? <a href="/login">Login</a></p>
    </div>
    <script>
        document.getElementById('registerForm').addEventListener('submit', async (event) => {
            event.preventDefault();
            const form = event.target;
            const formData = new FormData(form);
            try {
                const response = await fetch('/register/', {
                    method: 'POST',
                    body: formData
                });
                const data = await response.json();
                const messageDiv = document.getElementById('message');
                if (response.ok) {
                    messageDiv.textContent = data.message;
                    messageDiv.className = 'alert alert-success';
                    form.reset();
                } else {
                    messageDiv.textContent = data.detail || 'Registration failed';
                    messageDiv.className = 'alert alert-danger';
                }
            } catch (error) {
                console.error('Registration error:', error);
                document.getElementById('message').textContent = 'An error occurred during registration.';
                document.getElementById('message').className = 'alert alert-danger';
            }
        });
    </script>
</body>

</html>