/**
 * E2E Test for Chat Mode Reload Persistence
 *
 * The chat schema guarantees that ChatMessage.content is stored
 * inline (NOT NULL) and `get_session_messages` returns the persisted
 * messages on session reload. The step-persistence dedup/emit symmetry
 * ensures what users see live == what reload reconstructs.
 *
 * Verified here:
 *   - Create a session + insert a user message via API.
 *   - Navigate to /chat/<session_id> with a fresh page load.
 *   - The previously-sent message bubble appears in the rendered DOM.
 *   - Reloading again still renders the same bubble (idempotent).
 *
 * No LLM required: messages are seeded via the API with
 * trigger_research=false.
 *
 * Prerequisites: Web server on http://127.0.0.1:5000 (override BASE_URL).
 */

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;

const TIMEOUTS = {
    navigation: isCI ? 60000 : 30000,
    selector: isCI ? 30000 : 10000,
};

const SCREENSHOTS_DIR = path.join(__dirname, '..', 'screenshots');

async function snap(page, name) {
    if (!fs.existsSync(SCREENSHOTS_DIR)) {
        fs.mkdirSync(SCREENSHOTS_DIR, { recursive: true });
    }
    try {
        await page.screenshot({
            path: path.join(
                SCREENSHOTS_DIR,
                `chat_reload_${name}_${Date.now()}.png`
            ),
            fullPage: true,
        });
    } catch (_) {}
}

async function getCsrf(page) {
    return page.evaluate(() => {
        const m = document.querySelector('meta[name="csrf-token"]');
        return m ? m.content : '';
    });
}

async function api(page, csrfOuter, urlOuter, methodOuter = 'GET', bodyOuter = null) {
    return page.evaluate(
        async ({ url, method, body, csrf }) => {
            const opts = {
                method,
                headers: {
                    'Content-Type': 'application/json',
                    'X-CSRFToken': csrf,
                },
            };
            if (body !== null) opts.body = JSON.stringify(body);
            const r = await fetch(url, opts);
            let data = null;
            try {
                data = await r.json();
            } catch (_) {}
            return { ok: r.ok, status: r.status, data };
        },
        { url: urlOuter, method: methodOuter, body: bodyOuter, csrf: csrfOuter }
    );
}

async function countUserBubbles(page, needle) {
    return page.$$eval(
        '.ldr-chat-message-user .ldr-chat-message-text',
        (els, n) =>
            els.filter((e) => (e.textContent || '').includes(n)).length,
        needle
    );
}

async function run() {
    console.log(`Running chat reload-persistence tests (CI mode: ${isCI})`);

    const browser = await puppeteer.launch(getPuppeteerLaunchOptions());
    const page = await browser.newPage();
    await page.setViewport({ width: 1280, height: 800 });
    if (isCI) {
        page.setDefaultTimeout(60000);
        page.setDefaultNavigationTimeout(60000);
    }

    page.on('pageerror', (e) => console.log('PAGE ERROR:', e.message));
    page.on('console', (m) => {
        if (m.type() === 'error') console.log('BROWSER ERROR:', m.text());
    });

    const auth = new AuthHelper(page, BASE_URL);

    let passed = 0;
    let failed = 0;

    try {
        await auth.ensureAuthenticated();
        await page.goto(`${BASE_URL}/chat/`, {
            waitUntil: 'domcontentloaded',
            timeout: TIMEOUTS.navigation,
        });
        await page.waitForSelector('.ldr-chat-container', { timeout: TIMEOUTS.selector });
        const csrf = await getCsrf(page);

        // Seed: create session + two distinct user messages so we can
        // verify ordering survives a reload.
        const created = await api(page, csrf, '/api/chat/sessions', 'POST', {
            initial_query: 'reload-persistence-test seed',
        });
        if (!created.ok || !created.data?.success) {
            throw new Error(
                `Session creation failed: ${created.status} ${JSON.stringify(created.data)}`
            );
        }
        const sessionId = created.data.session_id;

        const probeA = `reload probe A ${Date.now()}`;
        const probeB = `reload probe B ${Date.now()}`;
        await api(
            page,
            csrf,
            `/api/chat/sessions/${sessionId}/messages`,
            'POST',
            { content: probeA, trigger_research: false }
        );
        await api(
            page,
            csrf,
            `/api/chat/sessions/${sessionId}/messages`,
            'POST',
            { content: probeB, trigger_research: false }
        );

        // Test 1: navigating to /chat/<id> renders both seeded messages.
        console.log('Test 1: fresh load of session renders seeded messages');
        try {
            await page.goto(`${BASE_URL}/chat/${sessionId}`, {
                waitUntil: 'domcontentloaded',
                timeout: TIMEOUTS.navigation,
            });
            await page.waitForSelector('.ldr-chat-container', {
                timeout: TIMEOUTS.selector,
            });
            // Wait for both bubbles. loadSession is async.
            await page.waitForFunction(
                ({ a, b }) => {
                    const nodes = document.querySelectorAll(
                        '.ldr-chat-message-user .ldr-chat-message-text'
                    );
                    const texts = Array.from(nodes).map((n) => n.textContent || '');
                    return (
                        texts.some((t) => t.includes(a)) &&
                        texts.some((t) => t.includes(b))
                    );
                },
                { timeout: TIMEOUTS.selector },
                { a: probeA, b: probeB }
            );
            console.log('PASSED');
            passed++;
        } catch (e) {
            console.log(`FAILED: ${e.message}`);
            await snap(page, 'fresh_load');
            failed++;
        }

        // Test 2: reload the page again — messages still there, exactly
        //         one bubble each (no double-rendering).
        console.log('Test 2: reload renders messages exactly once');
        try {
            await page.reload({
                waitUntil: 'domcontentloaded',
                timeout: TIMEOUTS.navigation,
            });
            await page.waitForSelector('.ldr-chat-container', {
                timeout: TIMEOUTS.selector,
            });
            await page.waitForFunction(
                (needle) => {
                    const nodes = document.querySelectorAll(
                        '.ldr-chat-message-user .ldr-chat-message-text'
                    );
                    return Array.from(nodes).some((n) =>
                        (n.textContent || '').includes(needle)
                    );
                },
                { timeout: TIMEOUTS.selector },
                probeA
            );

            const aCount = await countUserBubbles(page, probeA);
            const bCount = await countUserBubbles(page, probeB);
            if (aCount !== 1 || bCount !== 1) {
                throw new Error(
                    `Expected exactly one bubble each; got A=${aCount}, B=${bCount}`
                );
            }
            console.log('PASSED');
            passed++;
        } catch (e) {
            console.log(`FAILED: ${e.message}`);
            await snap(page, 'reload');
            failed++;
        }

        // Test 3: welcome screen is hidden on a session with messages
        //         (regression check for "empty session shows welcome").
        console.log('Test 3: welcome hidden on session with messages');
        try {
            const hidden = await page.$eval(
                '#chat-welcome',
                (el) => !el || getComputedStyle(el).display === 'none'
            );
            if (!hidden) throw new Error('Welcome screen visible on session with messages');
            console.log('PASSED');
            passed++;
        } catch (e) {
            console.log(`FAILED: ${e.message}`);
            await snap(page, 'welcome_hidden');
            failed++;
        }
    } catch (e) {
        console.log(`Test suite error: ${e.message}`);
        failed++;
    } finally {
        await browser.close();
    }

    console.log('-'.repeat(50));
    console.log(`Chat Reload Persistence Tests — passed: ${passed}, failed: ${failed}`);
    console.log('-'.repeat(50));
    if (failed > 0) process.exit(1);
}

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