<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Activating Account...</title>
    <style>
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
        }
        
        body {
            font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            min-height: 100vh;
            display: flex;
            align-items: center;
            justify-content: center;
            padding: 20px;
        }
        
        .container {
            background: white;
            border-radius: 20px;
            box-shadow: 0 20px 60px rgba(0,0,0,0.3);
            padding: 3rem;
            max-width: 500px;
            width: 100%;
            text-align: center;
        }
        
        .spinner {
            border: 4px solid #f3f3f3;
            border-top: 4px solid #667eea;
            border-radius: 50%;
            width: 60px;
            height: 60px;
            animation: spin 1s linear infinite;
            margin: 0 auto 2rem;
        }
        
        @keyframes spin {
            0% { transform: rotate(0deg); }
            100% { transform: rotate(360deg); }
        }
        
        .success {
            display: none;
        }
        
        .success-icon {
            font-size: 64px;
            margin-bottom: 1rem;
        }
        
        .error {
            display: none;
        }
        
        .error-icon {
            font-size: 64px;
            margin-bottom: 1rem;
        }
        
        h1 {
            color: #333;
            margin-bottom: 1rem;
        }
        
        p {
            color: #666;
            margin-bottom: 2rem;
        }
        
        .btn {
            display: inline-block;
            padding: 1rem 2rem;
            background: #667eea;
            color: white;
            text-decoration: none;
            border-radius: 8px;
            font-weight: 600;
            transition: background 0.3s;
        }
        
        .btn:hover {
            background: #5568d3;
        }
    </style>
</head>
<body>
    <div class="container">
        <div id="loading">
            <div class="spinner"></div>
            <h1>Activating Your Account...</h1>
            <p>Please wait while we activate your account.</p>
        </div>
        
        <div id="success" class="success">
            <div class="success-icon">✅</div>
            <h1>Account Activated!</h1>
            <p>Your account has been successfully activated. You can now log in.</p>
            <a href="/api/v1/spa/" class="btn">Go to Login</a>
        </div>
        
        <div id="error" class="error">
            <div class="error-icon">❌</div>
            <h1>Activation Failed</h1>
            <p id="errorMessage">The activation link is invalid or has expired.</p>
            <a href="/api/v1/spa/" class="btn">Back to Login</a>
        </div>
    </div>
    
    <script>
        // Get uid and token from Django template context
        const finalUid = '{{ uid }}';
        const finalToken = '{{ token }}';
        
        console.log('Activating with:', { uid: finalUid, token: finalToken });
        
        // Activate account
        async function activateAccount() {
            try {
                const response = await fetch('http://localhost:8000/api/v1/auth/users/activation/', {
                    method: 'POST',
                    headers: {
                        'Content-Type': 'application/json',
                    },
                    body: JSON.stringify({
                        uid: finalUid,
                        token: finalToken
                    })
                });
                
                document.getElementById('loading').style.display = 'none';
                
                if (response.ok || response.status === 204) {
                    document.getElementById('success').style.display = 'block';
                } else {
                    const data = await response.json().catch(() => ({}));
                    document.getElementById('error').style.display = 'block';
                    if (data.detail || data.token || data.uid) {
                        document.getElementById('errorMessage').textContent = 
                            data.detail || data.token?.[0] || data.uid?.[0] || 'Activation failed.';
                    }
                }
            } catch (error) {
                console.error('Activation error:', error);
                document.getElementById('loading').style.display = 'none';
                document.getElementById('error').style.display = 'block';
                document.getElementById('errorMessage').textContent = 'Network error. Please try again.';
            }
        }
        
        // Start activation
        if (finalUid && finalToken) {
            activateAccount();
        } else {
            document.getElementById('loading').style.display = 'none';
            document.getElementById('error').style.display = 'block';
            document.getElementById('errorMessage').textContent = 'Missing activation parameters.';
        }
    </script>
</body>
</html>

