/**
 * E2E Tests for Chat Session Management
 * Tests creating new sessions and session history
 *
 * Prerequisites: Web server running on http://127.0.0.1:5000
 */

const puppeteer = require('puppeteer');
const AuthHelper = require('../auth_helper');
const { getPuppeteerLaunchOptions } = require('../puppeteer_config');
const fs = require('fs');
const path = require('path');

const BASE_URL = process.env.BASE_URL || 'http://127.0.0.1:5000';
const isCI = !!process.env.CI;

// Timeouts adjusted for CI environment
const TIMEOUTS = {
    navigation: isCI ? 60000 : 30000,
    selector: isCI ? 30000 : 10000,
    element: isCI ? 10000 : 5000,
};

async function testChatSessionManagement() {
    console.log(`🧪 Running chat session management test (CI mode: ${isCI})`);

    // Create screenshots directory if it doesn't exist
    const screenshotsDir = path.join(__dirname, '..', 'screenshots');
    if (!fs.existsSync(screenshotsDir)) {
        fs.mkdirSync(screenshotsDir, { recursive: true });
    }

    const browser = await puppeteer.launch(getPuppeteerLaunchOptions());
    const page = await browser.newPage();

    // Set viewport
    await page.setViewport({ width: 1280, height: 800 });

    // Set timeouts
    if (isCI) {
        page.setDefaultTimeout(60000);
        page.setDefaultNavigationTimeout(60000);
    }

    // Enable console logging for debugging
    page.on('console', msg => {
        if (msg.type() === 'error') {
            console.log(`BROWSER ERROR: ${msg.text()}`);
        }
    });

    page.on('pageerror', error => {
        console.log('PAGE ERROR:', error.message);
    });

    const authHelper = new AuthHelper(page, BASE_URL);

    let testsPassed = 0;
    let testsFailed = 0;

    try {
        // Ensure we're logged in
        console.log('🔐 Ensuring authentication...');
        await authHelper.ensureAuthenticated();
        console.log('✅ Authentication verified\n');

        // Navigate to chat page to get CSRF token
        await page.goto(`${BASE_URL}/chat/`, {
            waitUntil: 'domcontentloaded',
            timeout: TIMEOUTS.navigation
        });

        await page.waitForSelector('.ldr-chat-container', {
            timeout: TIMEOUTS.selector
        });

        // Get CSRF token from the page
        const csrfToken = await page.evaluate(() => {
            const meta = document.querySelector('meta[name="csrf-token"]');
            if (meta) return meta.content;
            const input = document.querySelector('input[name="csrf_token"]');
            if (input) return input.value;
            return null;
        });

        if (!csrfToken) {
            console.log('⚠️  Warning: CSRF token not found, tests may fail');
        } else {
            console.log('✅ CSRF token obtained\n');
        }

        // Test 1: Create new session via API
        console.log('📋 Test 1: Create new session via API');
        try {
            // Create a session via the API directly
            const sessionResponse = await page.evaluate(async (token) => {
                const response = await fetch('/api/chat/sessions', {
                    method: 'POST',
                    headers: {
                        'Content-Type': 'application/json',
                        'X-CSRFToken': token || ''
                    },
                    credentials: 'same-origin',
                    body: JSON.stringify({ initial_query: 'Test session' })
                });
                return response.json();
            }, csrfToken);

            if (!sessionResponse.success || !sessionResponse.session_id) {
                throw new Error(`Failed to create session: ${JSON.stringify(sessionResponse)}`);
            }

            console.log(`   Created session: ${sessionResponse.session_id.substring(0, 8)}...`);
            console.log('✅ Test 1 PASSED: Session created via API\n');
            testsPassed++;
        } catch (error) {
            console.log(`❌ Test 1 FAILED: ${error.message}\n`);
            await page.screenshot({ path: path.join(screenshotsDir, 'chat_session_create_failed.png') });
            testsFailed++;
        }

        // Test 2: Session appears in list
        console.log('📋 Test 2: Session appears in session list');
        try {
            // First create a session
            const createResponse = await page.evaluate(async (token) => {
                const response = await fetch('/api/chat/sessions', {
                    method: 'POST',
                    headers: {
                        'Content-Type': 'application/json',
                        'X-CSRFToken': token || ''
                    },
                    credentials: 'same-origin',
                    body: JSON.stringify({ initial_query: 'Test for listing' })
                });
                return response.json();
            }, csrfToken);

            if (!createResponse.success) {
                throw new Error('Failed to create session for listing test');
            }

            const sessionId = createResponse.session_id;

            // Now get the session list
            const listResponse = await page.evaluate(async () => {
                const response = await fetch('/api/chat/sessions?status=active');
                return response.json();
            });

            if (!listResponse.success) {
                throw new Error('Failed to get session list');
            }

            const sessionInList = listResponse.sessions.some(s => s.id === sessionId);
            if (!sessionInList) {
                throw new Error(`Session ${sessionId} not found in list`);
            }

            console.log(`   Found ${listResponse.sessions.length} sessions in list`);
            console.log('✅ Test 2 PASSED: Session appears in list\n');
            testsPassed++;
        } catch (error) {
            console.log(`❌ Test 2 FAILED: ${error.message}\n`);
            await page.screenshot({ path: path.join(screenshotsDir, 'chat_session_list_failed.png') });
            testsFailed++;
        }

        // Test 3: Can start new chat (UI check)
        console.log('📋 Test 3: Chat page ready for new session');
        try {
            await page.goto(`${BASE_URL}/chat/`, {
                waitUntil: 'domcontentloaded',
                timeout: TIMEOUTS.navigation
            });

            await page.waitForSelector('.ldr-chat-container', {
                timeout: TIMEOUTS.selector
            });

            // Verify input is available for new messages
            const inputSelectors = [
                'textarea.chat-input',
                'input.chat-input',
                '#chat-input',
                '.ldr-chat-input textarea',
                '.ldr-chat-container textarea'
            ];

            let inputFound = false;
            for (const selector of inputSelectors) {
                const input = await page.$(selector);
                if (input) {
                    inputFound = true;
                    console.log(`   Found input with selector: ${selector}`);
                    break;
                }
            }

            if (!inputFound) {
                throw new Error('No input field found for new chat');
            }

            console.log('✅ Test 3 PASSED: Chat page ready for new session\n');
            testsPassed++;
        } catch (error) {
            console.log(`❌ Test 3 FAILED: ${error.message}\n`);
            await page.screenshot({ path: path.join(screenshotsDir, 'chat_new_session_failed.png') });
            testsFailed++;
        }

        // Test 4: Update session title via API
        console.log('📋 Test 4: Update session title via API');
        try {
            // Create a session first
            const createResponse = await page.evaluate(async (token) => {
                const response = await fetch('/api/chat/sessions', {
                    method: 'POST',
                    headers: {
                        'Content-Type': 'application/json',
                        'X-CSRFToken': token || ''
                    },
                    credentials: 'same-origin',
                    body: JSON.stringify({ initial_query: 'Title update test' })
                });
                return response.json();
            }, csrfToken);

            if (!createResponse.success) {
                throw new Error('Failed to create session');
            }

            const sessionId = createResponse.session_id;
            const newTitle = 'Updated Test Title';

            // Update the title
            const updateResponse = await page.evaluate(async (sid, title, token) => {
                const response = await fetch(`/api/chat/sessions/${sid}`, {
                    method: 'PATCH',
                    headers: {
                        'Content-Type': 'application/json',
                        'X-CSRFToken': token || ''
                    },
                    credentials: 'same-origin',
                    body: JSON.stringify({ title })
                });
                return response.json();
            }, sessionId, newTitle, csrfToken);

            if (!updateResponse.success) {
                throw new Error(`Failed to update title: ${JSON.stringify(updateResponse)}`);
            }

            // Verify the title was updated
            const getResponse = await page.evaluate(async (sid) => {
                const response = await fetch(`/api/chat/sessions/${sid}`);
                return response.json();
            }, sessionId);

            if (getResponse.session.title !== newTitle) {
                throw new Error(`Title mismatch: expected "${newTitle}", got "${getResponse.session.title}"`);
            }

            console.log(`   Title updated to: ${newTitle}`);
            console.log('✅ Test 4 PASSED: Session title updated\n');
            testsPassed++;
        } catch (error) {
            console.log(`❌ Test 4 FAILED: ${error.message}\n`);
            await page.screenshot({ path: path.join(screenshotsDir, 'chat_title_update_failed.png') });
            testsFailed++;
        }

        // Test 5: Archive session via API
        console.log('📋 Test 5: Archive session via API');
        try {
            // Create a session
            const createResponse = await page.evaluate(async (token) => {
                const response = await fetch('/api/chat/sessions', {
                    method: 'POST',
                    headers: {
                        'Content-Type': 'application/json',
                        'X-CSRFToken': token || ''
                    },
                    credentials: 'same-origin',
                    body: JSON.stringify({ initial_query: 'Archive test session' })
                });
                return response.json();
            }, csrfToken);

            const sessionId = createResponse.session_id;

            // Archive the session
            const archiveResponse = await page.evaluate(async (sid, token) => {
                const response = await fetch(`/api/chat/sessions/${sid}`, {
                    method: 'PATCH',
                    headers: {
                        'Content-Type': 'application/json',
                        'X-CSRFToken': token || ''
                    },
                    credentials: 'same-origin',
                    body: JSON.stringify({ status: 'archived' })
                });
                return response.json();
            }, sessionId, csrfToken);

            if (!archiveResponse.success) {
                throw new Error('Failed to archive session');
            }

            // Verify session is archived
            const getResponse = await page.evaluate(async (sid) => {
                const response = await fetch(`/api/chat/sessions/${sid}`);
                return response.json();
            }, sessionId);

            if (getResponse.session.status !== 'archived') {
                throw new Error(`Status mismatch: expected "archived", got "${getResponse.session.status}"`);
            }

            // Verify not in active list
            const activeList = await page.evaluate(async () => {
                const response = await fetch('/api/chat/sessions?status=active');
                return response.json();
            });

            const inActiveList = activeList.sessions.some(s => s.id === sessionId);
            if (inActiveList) {
                throw new Error('Archived session still appears in active list');
            }

            console.log('   Session archived and removed from active list');
            console.log('✅ Test 5 PASSED: Session archived\n');
            testsPassed++;
        } catch (error) {
            console.log(`❌ Test 5 FAILED: ${error.message}\n`);
            await page.screenshot({ path: path.join(screenshotsDir, 'chat_archive_failed.png') });
            testsFailed++;
        }

        // Test 6: Delete session via API
        console.log('📋 Test 6: Delete session via API');
        try {
            // Create a session
            const createResponse = await page.evaluate(async (token) => {
                const response = await fetch('/api/chat/sessions', {
                    method: 'POST',
                    headers: {
                        'Content-Type': 'application/json',
                        'X-CSRFToken': token || ''
                    },
                    credentials: 'same-origin',
                    body: JSON.stringify({ initial_query: 'Delete test session' })
                });
                return response.json();
            }, csrfToken);

            const sessionId = createResponse.session_id;

            // Delete the session
            const deleteResponse = await page.evaluate(async (sid, token) => {
                const response = await fetch(`/api/chat/sessions/${sid}`, {
                    method: 'DELETE',
                    headers: {
                        'X-CSRFToken': token || ''
                    },
                    credentials: 'same-origin'
                });
                return response.json();
            }, sessionId, csrfToken);

            if (!deleteResponse.success) {
                throw new Error('Failed to delete session');
            }

            // Verify not in active list
            const activeList = await page.evaluate(async () => {
                const response = await fetch('/api/chat/sessions?status=active');
                return response.json();
            });

            const inActiveList = activeList.sessions.some(s => s.id === sessionId);
            if (inActiveList) {
                throw new Error('Deleted session still appears in active list');
            }

            console.log('   Session deleted successfully');
            console.log('✅ Test 6 PASSED: Session deleted\n');
            testsPassed++;
        } catch (error) {
            console.log(`❌ Test 6 FAILED: ${error.message}\n`);
            await page.screenshot({ path: path.join(screenshotsDir, 'chat_delete_failed.png') });
            testsFailed++;
        }

        // Test 7: Get single session details
        console.log('📋 Test 7: Get single session details');
        try {
            // Create a session
            const createResponse = await page.evaluate(async (token) => {
                const response = await fetch('/api/chat/sessions', {
                    method: 'POST',
                    headers: {
                        'Content-Type': 'application/json',
                        'X-CSRFToken': token || ''
                    },
                    credentials: 'same-origin',
                    body: JSON.stringify({ initial_query: 'Details test session' })
                });
                return response.json();
            }, csrfToken);

            const sessionId = createResponse.session_id;

            // Get session details
            const getResponse = await page.evaluate(async (sid) => {
                const response = await fetch(`/api/chat/sessions/${sid}`);
                return response.json();
            }, sessionId);

            if (!getResponse.success) {
                throw new Error('Failed to get session details');
            }

            // Verify expected fields
            const session = getResponse.session;
            const requiredFields = ['id', 'title', 'status', 'created_at', 'message_count'];
            for (const field of requiredFields) {
                if (!(field in session)) {
                    throw new Error(`Missing field: ${field}`);
                }
            }

            console.log(`   Session has ${requiredFields.length} required fields`);
            console.log('✅ Test 7 PASSED: Session details retrieved\n');
            testsPassed++;
        } catch (error) {
            console.log(`❌ Test 7 FAILED: ${error.message}\n`);
            await page.screenshot({ path: path.join(screenshotsDir, 'chat_get_session_failed.png') });
            testsFailed++;
        }

        // Test 8: Session with special characters in initial query
        console.log('📋 Test 8: Session with special characters');
        try {
            const specialQuery = 'Test with "quotes" & <tags> and émojis 🎉';

            const createResponse = await page.evaluate(async (query, token) => {
                const response = await fetch('/api/chat/sessions', {
                    method: 'POST',
                    headers: {
                        'Content-Type': 'application/json',
                        'X-CSRFToken': token || ''
                    },
                    credentials: 'same-origin',
                    body: JSON.stringify({ initial_query: query })
                });
                return response.json();
            }, specialQuery, csrfToken);

            if (!createResponse.success) {
                throw new Error('Failed to create session with special characters');
            }

            // Verify session was created
            const getResponse = await page.evaluate(async (sid) => {
                const response = await fetch(`/api/chat/sessions/${sid}`);
                return response.json();
            }, createResponse.session_id);

            if (!getResponse.success) {
                throw new Error('Failed to retrieve session');
            }

            console.log('   Session with special characters created successfully');
            console.log('✅ Test 8 PASSED: Special characters handled\n');
            testsPassed++;
        } catch (error) {
            console.log(`❌ Test 8 FAILED: ${error.message}\n`);
            await page.screenshot({ path: path.join(screenshotsDir, 'chat_special_chars_failed.png') });
            testsFailed++;
        }

    } catch (error) {
        console.log(`💥 Test suite error: ${error.message}`);
        testsFailed++;
    } finally {
        await browser.close();
    }

    // Print summary
    console.log('─'.repeat(50));
    console.log(`📊 Chat Session Management Tests Summary`);
    console.log(`   ✅ Passed: ${testsPassed}`);
    console.log(`   ❌ Failed: ${testsFailed}`);
    console.log('─'.repeat(50));

    if (testsFailed > 0) {
        process.exit(1);
    }
}

testChatSessionManagement().catch(error => {
    console.error('💥 Test runner error:', error);
    process.exit(1);
});
