<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Login - API Key Management Service</title>
    <style>
        body {
            font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            margin: 0;
            padding: 20px;
            display: flex;
            justify-content: center;
            align-items: center;
            min-height: 100vh;
        }
        .container {
            background: white;
            border-radius: 10px;
            box-shadow: 0 15px 35px rgba(0,0,0,0.1);
            padding: 40px;
            max-width: 500px;
            width: 100%;
            text-align: center;
        }
        h1 {
            color: #333;
            margin-bottom: 30px;
            font-size: 28px;
            font-weight: 300;
        }
        .token-section {
            background: #f8f9fa;
            border: 1px solid #e9ecef;
            border-radius: 8px;
            padding: 20px;
            margin: 20px 0;
        }
        .token-display {
            background: #2d3748;
            color: #e2e8f0;
            padding: 15px;
            border-radius: 6px;
            font-family: 'Courier New', monospace;
            font-size: 14px;
            word-break: break-all;
            margin: 15px 0;
            border: none;
            width: 100%;
            min-height: 60px;
            resize: none;
        }
        .copy-button {
            background: #4299e1;
            color: white;
            border: none;
            padding: 10px 20px;
            border-radius: 5px;
            cursor: pointer;
            font-size: 14px;
            margin-top: 10px;
            transition: background-color 0.2s;
        }
        .copy-button:hover {
            background: #3182ce;
        }
        .status {
            margin: 20px 0;
            padding: 15px;
            border-radius: 6px;
            font-weight: 500;
        }
        .status.success {
            background: #d4edda;
            color: #155724;
            border: 1px solid #c3e6cb;
        }
        .status.error {
            background: #f8d7da;
            color: #721c24;
            border: 1px solid #f5c6cb;
        }
        .status.info {
            background: #d1ecf1;
            color: #0c5460;
            border: 1px solid #bee5eb;
        }
        .spinner {
            border: 3px solid #f3f3f3;
            border-top: 3px solid #4299e1;
            border-radius: 50%;
            width: 30px;
            height: 30px;
            animation: spin 1s linear infinite;
            margin: 0 auto 15px auto;
        }
        @keyframes spin {
            0% { transform: rotate(0deg); }
            100% { transform: rotate(360deg); }
        }
        .hidden {
            display: none;
        }
        .redirect-info {
            font-size: 14px;
            color: #666;
            margin-top: 15px;
        }
        noscript .no-js-message {
            background: #fff3cd;
            color: #856404;
            padding: 15px;
            border: 1px solid #ffeaa7;
            border-radius: 6px;
            margin-bottom: 20px;
        }
    </style>
</head>
<body>
    <div class="container">
        <h1>🔐 Login Success</h1>
        
        <noscript>
            <div class="no-js-message">
                <strong>JavaScript is disabled.</strong> Please copy the token below and paste it into your application.
            </div>
        </noscript>

        <div id="loading" class="status info">
            <div class="spinner"></div>
            <div>Processing login and redirecting...</div>
        </div>

        <div id="success-status" class="status success hidden">
            ✅ Login successful! Redirecting to your application...
        </div>

        <div id="error-status" class="status error hidden">
            <div id="error-message">❌ Redirect failed. Please copy the token below manually.</div>
        </div>

        <div class="token-section">
            <h3>Your Authentication Token</h3>
            <p>Use this token to authenticate with the API:</p>
            <textarea id="token-display" class="token-display" readonly>{{.Token}}</textarea>
            <button id="copy-button" class="copy-button" onclick="copyToken()">📋 Copy Token</button>
            <div class="redirect-info">
                <strong>Token expires:</strong> {{.ExpiresAt}}<br>
                <strong>Application:</strong> {{.AppID}}<br>
                <strong>User:</strong> {{.UserID}}
            </div>
        </div>
    </div>

    <script>
        // Token and redirect information from server
        const token = {{.TokenJSON}};
        const redirectURL = {{.RedirectURLJSON}};
        
        // Elements
        const loadingDiv = document.getElementById('loading');
        const successDiv = document.getElementById('success-status');
        const errorDiv = document.getElementById('error-status');
        const errorMessage = document.getElementById('error-message');

        function copyToken() {
            const tokenDisplay = document.getElementById('token-display');
            tokenDisplay.select();
            tokenDisplay.setSelectionRange(0, 99999); // For mobile devices
            
            try {
                document.execCommand('copy');
                const copyButton = document.getElementById('copy-button');
                const originalText = copyButton.textContent;
                copyButton.textContent = '✅ Copied!';
                copyButton.style.background = '#48bb78';
                
                setTimeout(() => {
                    copyButton.textContent = originalText;
                    copyButton.style.background = '#4299e1';
                }, 2000);
            } catch (err) {
                console.error('Failed to copy token:', err);
            }
        }

        function performRedirect() {
            if (!redirectURL) {
                // No redirect URL provided, just show success message
                loadingDiv.classList.add('hidden');
                successDiv.innerHTML = '✅ Login successful! You can now close this window and use the token above.';
                successDiv.classList.remove('hidden');
                return;
            }

            try {
                // Show success status
                loadingDiv.classList.add('hidden');
                successDiv.classList.remove('hidden');
                
                // Perform the redirect after a short delay
                setTimeout(() => {
                    window.location.href = redirectURL;
                }, 1500);
                
                // Set a backup timer in case redirect fails
                setTimeout(() => {
                    if (window.location.href.indexOf(redirectURL) === -1) {
                        // Redirect failed, show error
                        successDiv.classList.add('hidden');
                        errorDiv.classList.remove('hidden');
                        errorMessage.textContent = '❌ Automatic redirect failed. Please copy the token above and paste it into your application.';
                    }
                }, 5000);
                
            } catch (error) {
                console.error('Redirect failed:', error);
                loadingDiv.classList.add('hidden');
                errorDiv.classList.remove('hidden');
                errorMessage.textContent = '❌ Redirect failed: ' + error.message + '. Please copy the token above manually.';
            }
        }

        // Start the redirect process when page loads
        document.addEventListener('DOMContentLoaded', function() {
            // Small delay to let the user see the page loaded
            setTimeout(performRedirect, 1000);
        });

        // Token is always delivered via query parameter
    </script>
</body>
</html>