<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>{{brand_name}} - AI Testing Agents</title>
    <style>
        :root {
            --dark-blue: #111827;
            --green: #3CDF7C;
            --orange: #FF8A3D;
            --light-gray: #A1A1AA;
        }
        
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
            font-family: 'Inter', 'Segoe UI', Roboto, Arial, sans-serif;
        }
        
        body {
            background-color: var(--dark-blue);
            color: white;
            line-height: 1.6;
        }
        
        .container {
            width: 100%;
            max-width: 1200px;
            margin: 0 auto;
            padding: 0 20px;
        }
        
        header {
            padding: 15px 0;
        }
        
        .logo {
            display: flex;
            align-items: center;
        }
        
        .logo img {
            height: 40px;
        }
        
        .logo-text {
            margin-left: 10px;
            font-size: 24px;
            font-weight: 600;
            color: white;
        }
        
        .hero {
            padding: 20px 0 30px;
            text-align: center;
        }
        
        .hero h1 {
            font-size: 48px;
            font-weight: 700;
            margin-bottom: 10px;
            background: linear-gradient(90deg, var(--green) 0%, var(--orange) 100%);
            -webkit-background-clip: text;
            background-clip: text;
            color: transparent;
            display: inline-block;
        }
        
        .hero p {
            font-size: 22px;
            margin-bottom: 5px;
        }
        
        .hero .subtitle {
            font-size: 16px;
            color: var(--light-gray);
        }
        
        /* Chatbot Interface Styles */
        .chatbot-container {
            max-width: 800px;
            margin: 20px auto 60px;
            background-color: rgba(30, 41, 59, 0.7);
            border-radius: 12px;
            overflow: hidden;
            box-shadow: 0 10px 25px rgba(0, 0, 0, 0.2);
            border: 1px solid rgba(255, 255, 255, 0.1);
        }
        
        .chat-header {
            padding: 15px 20px;
            background-color: rgba(15, 23, 42, 0.6);
            display: flex;
            align-items: center;
            justify-content: space-between;
            border-bottom: 1px solid rgba(255, 255, 255, 0.1);
        }
        
        .chat-header h3 {
            font-size: 16px;
            font-weight: 500;
        }
        
        .chat-header .status {
            display: flex;
            align-items: center;
        }
        
        .status-indicator {
            width: 8px;
            height: 8px;
            background-color: var(--green);
            border-radius: 50%;
            margin-right: 6px;
        }
        
        .chat-messages {
            height: 300px;
            overflow-y: auto;
            padding: 20px;
        }
        
        .message {
            margin-bottom: 20px;
            display: flex;
            align-items: flex-start;
        }
        
        .message.ai {
            justify-content: flex-start;
        }
        
        .message.user {
            justify-content: flex-end;
        }
        
        .avatar {
            width: 30px;
            height: 30px;
            border-radius: 6px;
            margin-right: 12px;
            display: flex;
            align-items: center;
            justify-content: center;
            font-weight: bold;
            font-size: 14px;
        }
        
        .avatar.ai {
            background: linear-gradient(135deg, var(--green), var(--orange));
            overflow: hidden;
        }
        
        .avatar.ai img {
            width: 100%;
            height: 100%;
            object-fit: cover;
        }
        
        .avatar.user {
            background-color: #4B5563;
        }
        
        .message-content {
            max-width: 80%;
            padding: 12px 16px;
            border-radius: 10px;
            font-size: 14px;
            line-height: 1.5;
        }
        
        .ai .message-content {
            background-color: rgba(15, 23, 42, 0.8);
            border: 1px solid rgba(255, 255, 255, 0.05);
        }
        
        .user .message-content {
            background-color: #3B82F6;
            color: white;
        }
        
        .chat-input {
            padding: 15px 20px;
            border-top: 1px solid rgba(255, 255, 255, 0.1);
            display: flex;
            align-items: center;
        }
        
        .chat-input input {
            flex: 1;
            padding: 12px 16px;
            border: none;
            border-radius: 8px;
            background-color: rgba(15, 23, 42, 0.6);
            color: white;
            font-size: 14px;
            outline: none;
        }
        
        .chat-input input::placeholder {
            color: rgba(255, 255, 255, 0.5);
        }
        
        .send-button {
            width: 40px;
            height: 40px;
            border-radius: 8px;
            background: linear-gradient(135deg, var(--green), var(--orange));
            border: none;
            margin-left: 10px;
            cursor: pointer;
            display: flex;
            align-items: center;
            justify-content: center;
            transition: all 0.2s ease;
        }
        
        .send-button:hover {
            opacity: 0.9;
        }
        
        /* Quick chat buttons styles */
        .quick-chat-buttons {
            display: flex;
            flex-wrap: wrap;
            gap: 10px;
            margin-top: 10px;
            padding: 0 10px;
        }
        
        .quick-chat-button {
            background: rgba(255, 255, 255, 0.1);
            border: 1px solid rgba(255, 255, 255, 0.2);
            border-radius: 20px;
            padding: 8px 15px;
            color: white;
            font-size: 14px;
            cursor: pointer;
            transition: all 0.2s ease;
            white-space: nowrap;
            overflow: hidden;
            text-overflow: ellipsis;
            max-width: 100%;
        }
        
        .quick-chat-button:hover {
            background: rgba(255, 255, 255, 0.2);
            border-color: var(--green);
        }

        /* Credentials section styles */
        .credentials-section {
            padding: 15px 20px;
            border-top: 1px solid rgba(255, 255, 255, 0.1);
            display: flex;
            flex-wrap: wrap;
            gap: 10px;
        }
        
        .credentials-field {
            flex: 1;
            min-width: 200px;
        }
        
        .credentials-field label {
            display: block;
            font-size: 12px;
            color: var(--light-gray);
            margin-bottom: 5px;
        }
        
        .credentials-field input {
            width: 100%;
            padding: 8px 12px;
            border: none;
            border-radius: 6px;
            background-color: rgba(15, 23, 42, 0.6);
            color: white;
            font-size: 14px;
            outline: none;
        }
        
        .credentials-field input[type="password"] {
            font-size: 14px;
            letter-spacing: 2px;
        }
        
        @media (max-width: 768px) {
            .hero h1 {
                font-size: 42px;
            }
            
            .hero p {
                font-size: 20px;
            }
            
            .chatbot-container {
                margin: 20px 15px 40px;
            }
            
            .message-content {
                max-width: 90%;
            }
        }
    </style>
</head>
<body>
    <div class="container">
        <header>
            <div class="logo">
                <img src="{{brand_icon}}" alt="{{brand_name}} Logo">
                <span class="logo-text">{{brand_name}}</span>
            </div>
        </header>
        
        <section class="hero">
            <h1>AI Testing Agents</h1>
            <p>Just minutes to bugs and functional test coverage</p>
            <p class="subtitle">Made by the engineers who tested Google Chrome</p>
        </section>
        
        <section class="chatbot-container">
            <div class="chat-header">
                <h3>AI Testing Assistant</h3>
                <div class="status">
                    <div class="status-indicator"></div>
                    <span>Online</span>
                </div>
            </div>
            <div class="credentials-section">
                <div class="credentials-field">
                    <label for="team-name">Team Name:</label>
                    <input type="text" id="team-name" value="free">
                </div>
                <div class="credentials-field">
                    <label for="activation-code">Activation Code:</label>
                    <input type="password" id="activation-code" value="">
                </div>
            </div>
            <div class="chat-messages">
                <div class="message ai">
                    <div class="avatar ai">
                        <img src="/images/jason.jpg" alt="AI Avatar">
                    </div>
                    <div class="message-content">
                        Hello! I'm your AI Testing Agent assistant. Ask me to test something, how to use {{brand_name}}, check your website for bugs, or generate and execute functional tests for your website.
                    </div>
                </div>
            </div>
            <div class="chat-input">
                <input type="text" id="user-message" placeholder="Type your message here...">
                <button class="send-button" id="send-button">
                    <svg width="20" height="20" viewBox="0 0 24 24" fill="none" xmlns="http://www.w3.org/2000/svg">
                        <path d="M22 2L11 13" stroke="white" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"/>
                        <path d="M22 2L15 22L11 13L2 9L22 2Z" stroke="white" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"/>
                    </svg>
                </button>
            </div>
            <div class="quick-chat-buttons">
                <button class="quick-chat-button" data-text="Test that user can search for '{{brand_name}}' on Google.com and top result is {{brand_name}}">Test Google search for {{brand_name}}</button>
                <button class="quick-chat-button" data-text="Check for bugs on https://www.cnn.com">Check for bugs on CNN</button>
                <button class="quick-chat-button" data-text="Generate and execute functional bugs for https://www.bing.com">Test Bing for functional bugs</button>
                <button class="quick-chat-button" data-text="Who works at {{brand_name}}?">Who works at {{brand_name}}?</button>
                <button class="quick-chat-button" data-text="Tell me possible testers commands I can execute">Show testers commands</button>
            </div>
        </section>
    </div>

    <script>
        document.addEventListener('DOMContentLoaded', function() {
            // Focus on the chat input field when page loads
            const userInput = document.getElementById('user-message');
            userInput.focus();
            
            const chatMessages = document.querySelector('.chat-messages');
            const sendButton = document.getElementById('send-button');
            const teamNameInput = document.getElementById('team-name');
            const activationCodeInput = document.getElementById('activation-code');
            
            // Set default values for credentials
            const defaultTeam = 'free';
            const defaultActivationCode = '';
            
            // Initialize with default values
            teamNameInput.value = defaultTeam;
            // Don't show the actual code in the UI, but store it for use
            activationCodeInput.value = '';
            let currentActivationCode = defaultActivationCode;
            
            // Handle activation code input changes
            activationCodeInput.addEventListener('focus', function() {
                // Clear the field when focused
                this.value = '';
            });
            
            activationCodeInput.addEventListener('blur', function() {
                // If empty, restore default
                if (!this.value) {
                    this.value = '';
                    currentActivationCode = defaultActivationCode;
                } else {
                    // Save the new value but display as stars
                    currentActivationCode = this.value;
                    this.value = '•'.repeat(this.value.length);
                }
            });
            
            // Array to store chat history (last 10 messages)
            const chatHistory = [];
            
            // Function to handle quick chat buttons
            document.querySelectorAll('.quick-chat-button').forEach(button => {
                button.addEventListener('click', function() {
                    const text = this.getAttribute('data-text');
                    userInput.value = text;
                    userInput.focus();
                });
            });
            
            // Function to add a message to the chat
            function addMessage(content, isUser = false) {
                const messageDiv = document.createElement('div');
                messageDiv.className = isUser ? 'message user' : 'message ai';
                
                if (isUser) {
                    // User message
                    const avatar = document.createElement('div');
                    avatar.className = 'avatar user';
                    
                    const messageContent = document.createElement('div');
                    messageContent.className = 'message-content';
                    messageContent.innerHTML = content.replace(/\n/g, '<br>');
                    
                    //messageDiv.appendChild(avatar);
                    messageDiv.appendChild(messageContent);
                } else {
                    // AI message
                    const avatar = document.createElement('div');
                    avatar.className = 'avatar ai';
                    
                    const avatarImg = document.createElement('img');
                    avatarImg.src = 'images/jason.jpg';
                    avatarImg.alt = 'AI Avatar';
                    avatar.appendChild(avatarImg);
                    
                    const messageContent = document.createElement('div');
                    messageContent.className = 'message-content';
                    messageContent.innerHTML = content.replace(/\n/g, '<br>');
                    
                    messageDiv.appendChild(avatar);
                    messageDiv.appendChild(messageContent);
                }
                
                chatMessages.appendChild(messageDiv);
                
                // Store message in chat history
                chatHistory.push({
                    content: content.length > 1000 ? content.substring(0, 1000) + '...' : content,
                    isUser: isUser
                });
                
                // Keep only the last 10 messages
                if (chatHistory.length > 10) {
                    chatHistory.shift();
                }
                
                // Scroll to the bottom of the chat
                chatMessages.scrollTop = chatMessages.scrollHeight;
            }

            // Function to send message to API and handle response
            async function sendMessageToAPI(message) {
                try {
                    // Show loading indicator
                    const loadingDiv = document.createElement('div');
                    loadingDiv.className = 'message ai';
                    
                    const avatar = document.createElement('div');
                    avatar.className = 'avatar ai';
                    
                    const avatarImg = document.createElement('img');
                    avatarImg.src = 'images/jason.jpg';
                    avatarImg.alt = 'AI Avatar';
                    avatar.appendChild(avatarImg);
                    
                    const messageContent = document.createElement('div');
                    messageContent.className = 'message-content';
                    messageContent.textContent = 'Reasoning...';
                    
                    loadingDiv.appendChild(avatar);
                    loadingDiv.appendChild(messageContent);
                    
                    chatMessages.appendChild(loadingDiv);
                    chatMessages.scrollTop = chatMessages.scrollHeight;
                    
                    // Get current team and activation code values
                    const teamName = teamNameInput.value || defaultTeam;
                    const activationCode = currentActivationCode;
                    
                    // Call the API
                    const response = await fetch('http://localhost:5005/chat', {
                        method: 'POST',
                        headers: {
                            'Content-Type': 'application/json'
                        },
                        body: JSON.stringify({
                            message: message,
                            chat_history: chatHistory.slice(-10), // Send the last 5 chats and responses (10 messages total)
                            team: teamName,
                            activation_code: activationCode
                        })
                    });
                    
                    // Remove loading indicator
                    chatMessages.removeChild(loadingDiv);
                    
                    if (response.ok) {
                        const data = await response.json();
                        // Display the response from the API
                        console.log('data', data)
                        addMessage(data.summary.response_to_user || 'No response available', false);
                    } else {
                        // Handle error
                        addMessage('Sorry, I encountered an error while processing your request.', false);
                    }
                } catch (error) {
                    console.error('Error:', error);
                    addMessage('Sorry, I encountered an error while processing your request.', false);
                }
            }

            // Event listener for send button
            sendButton.addEventListener('click', function() {
                const message = userInput.value.trim();
                if (message) {
                    // Add user message to chat
                    addMessage(message, true);
                    
                    // Clear input field
                    userInput.value = '';
                    
                    // Send message to API
                    sendMessageToAPI(message);
                }
            });

            // Event listener for Enter key
            userInput.addEventListener('keypress', function(e) {
                if (e.key === 'Enter') {
                    const message = userInput.value.trim();
                    if (message) {
                        // Add user message to chat
                        addMessage(message, true);
                        
                        // Clear input field
                        userInput.value = '';
                        
                        // Send message to API
                        sendMessageToAPI(message);
                    }
                }
            });
        });
    </script>
</body>
</html>
