<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Request an account</title>
    <link rel="stylesheet" href="/static/styles.css">
</head>
<body>
    <div class="card">
        <div class="logo-container">
            <img src="/static/logo.png" alt="Logo" class="logo">
        </div>
        
        <div id="registration-form">
            <form action="/register" method="post">
                <input type="text" name="requested_username" placeholder="Requested Username" required>
                <input type="email" name="email" placeholder="Valid Email Address" required>
                <button type="submit">Request Registration Token</button>
            </form>
        </div>

        <div id="registration-closed" class="registration-closed" style="display: none;">
            Registration is currently closed
        </div>

        <p class="info-text">
            <span id="info-message">A registration token will be emailed to you.</span><br>
            Token resets at: <span id="countdown" class="countdown">--:--:--</span> UTC
        </p>
    </div>

    <script>
        // Configuration values provided by the server
        const TOKEN_RESET_HOUR = {{ token_reset_time_utc }};  // e.g., 0 for midnight UTC
        const DOWNTIME_MINUTES = {{ downtime_before_token_reset }};  // e.g., 30 minutes

        function updateCountdown() {
            const now = new Date();
            
            // Calculate next reset time
            const resetTime = new Date();
            resetTime.setUTCHours(TOKEN_RESET_HOUR, 0, 0, 0);
            if (now > resetTime) {
                resetTime.setDate(resetTime.getDate() + 1);
            }
            
            // Calculate when registration should close
            const downtimeStart = new Date(resetTime);
            downtimeStart.setMinutes(downtimeStart.getMinutes() - DOWNTIME_MINUTES);
            
            // Update form visibility based on whether we're in downtime
            const isRegistrationOpen = now < downtimeStart;
            document.getElementById('registration-form').style.display = 
                isRegistrationOpen ? 'block' : 'none';
            document.getElementById('registration-closed').style.display = 
                isRegistrationOpen ? 'none' : 'block';
            
            // Update info message based on registration status
            const infoMessage = document.getElementById('info-message');
            infoMessage.textContent = isRegistrationOpen 
                ? 'A registration token will be emailed to you.'
                : 'Please come back after the token resets.';
            
            // Always show countdown to reset time (not downtime)
            const timeUntilReset = resetTime - now;
            const hours = String(Math.floor(timeUntilReset / (1000 * 60 * 60))).padStart(2, '0');
            const minutes = String(Math.floor((timeUntilReset % (1000 * 60 * 60)) / (1000 * 60))).padStart(2, '0');
            const seconds = String(Math.floor((timeUntilReset % (1000 * 60)) / 1000)).padStart(2, '0');
            
            document.getElementById('countdown').textContent = `${hours}:${minutes}:${seconds}`;
        }

        // Update immediately and then every second
        updateCountdown();
        setInterval(updateCountdown, 1000);
    </script>
</body>
</html>