{% extends "base.html" %}

{% block content %}
<h1>Create an Account</h1>

<form method="POST">
    <div>
        <label for="username">Username</label>
        <input type="text" id="username" name="username" required>
    </div>
    
    <div>
        <label for="email">Email</label>
        <input type="email" id="email" name="email" required>
    </div>
    
    <div>
        <label for="password">Password</label>
        <input type="password" id="password" name="password" required>
        <small>Password must be at least 10 characters, contain 1 uppercase letter, 1 special character, and 1 number.</small>
    </div>

    <div>
        <label for="confirm_password">Confirm Password</label>
        <input type="password" id="confirm_password" name="confirm_password" required>
    </div>
    
    <button type="submit" id="submit" disabled>Create Account</button>
</form>

<div id="password-requirements">
    <ul>
        <li id="min-length">Minimum 10 characters: ❌</li>
        <li id="uppercase">At least 1 uppercase letter: ❌</li>
        <li id="special-char">At least 1 special character: ❌</li>
        <li id="number">At least 1 number: ❌</li>
        <li id="match">Passwords match: ❌</li>
    </ul>
</div>

<script>
    const passwordInput = document.getElementById('password');
    const confirmPasswordInput = document.getElementById('confirm_password');
    const submitButton = document.getElementById('submit');

    const minLengthRequirement = document.getElementById('min-length');
    const uppercaseRequirement = document.getElementById('uppercase');
    const specialCharRequirement = document.getElementById('special-char');
    const numberRequirement = document.getElementById('number');
    const matchRequirement = document.getElementById('match');

    function validatePassword() {
        const password = passwordInput.value;
        const confirmPassword = confirmPasswordInput.value;

        // Regex pattern for password validation
        const passwordPattern = /^(?=.*[A-Z])(?=.*\d)(?=.*[\W_]).{10,}$/;

        const passwordValid = passwordPattern.test(password);
        const passwordsMatch = password === confirmPassword;

        // Check each validation rule and update the corresponding list item
        minLengthRequirement.textContent = password.length >= 10 ? "Minimum 10 characters: ✔" : "Minimum 10 characters: ❌";
        uppercaseRequirement.textContent = /[A-Z]/.test(password) ? "At least 1 uppercase letter: ✔" : "At least 1 uppercase letter: ❌";
        specialCharRequirement.textContent = /[\W_]/.test(password) ? "At least 1 special character: ✔" : "At least 1 special character: ❌";
        numberRequirement.textContent = /\d/.test(password) ? "At least 1 number: ✔" : "At least 1 number: ❌";
        matchRequirement.textContent = passwordsMatch ? "Passwords match: ✔" : "Passwords match: ❌";

        // Enable submit button only if all conditions are met
        if (passwordValid && passwordsMatch) {
            submitButton.disabled = false;
        } else {
            submitButton.disabled = true;
        }
    }

    // Event listeners for password input and confirmation
    passwordInput.addEventListener('input', validatePassword);
    confirmPasswordInput.addEventListener('input', validatePassword);
</script>

{% endblock %}
