/**
 * E2E Test for Chat Mode Archived-Session Send Rejection
 *
 * send_message has a `status='active'` guard that returns
 * HTTP 409 with a friendly "This chat is archived. Reactivate it to
 * continue." error when a user tries to send into an archived (or
 * deleted) session. There is also an atomic enforcement at the
 * UPDATE...RETURNING level in insert_message_in_db, which we exercise
 * indirectly via the API.
 *
 * This test:
 *   1. Creates a session via API.
 *   2. PATCHes status to "archived".
 *   3. POSTs a message — expects HTTP 409 + error mentioning "archived".
 *   4. Reactivates the session (status="active").
 *   5. POSTs again — expects HTTP 200 (the post-archive guard releases).
 *
 * No LLM required: each test asserts on the HTTP envelope only and uses
 * trigger_research=false to avoid background research spawning.
 *
 * 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 });
    }
    const file = path.join(
        SCREENSHOTS_DIR,
        `chat_archived_${name}_${Date.now()}.png`
    );
    try {
        await page.screenshot({ path: file, 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 run() {
    console.log(`Running chat archived-session-reject 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();

        // Land on the chat page so we can pull the CSRF token from the
        // meta tag (same flow as the existing session-management test).
        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);
        if (!csrf) {
            console.log('FAILED: could not obtain CSRF token from chat page');
            process.exit(1);
        }

        // Create a session.
        const created = await api(page, csrf, '/api/chat/sessions', 'POST', {
            initial_query: 'archive-guard probe',
        });
        if (!created.ok || !created.data?.success) {
            throw new Error(
                `Session creation failed: status=${created.status} body=${JSON.stringify(created.data)}`
            );
        }
        const sessionId = created.data.session_id;

        // Test 1: Archive then send → expect 409 + "archived" in error.
        console.log('Test 1: archived session rejects send with 409');
        try {
            const archiveResp = await api(
                page,
                csrf,
                `/api/chat/sessions/${sessionId}`,
                'PATCH',
                { status: 'archived' }
            );
            if (!archiveResp.ok) {
                throw new Error(
                    `Archive PATCH failed: status=${archiveResp.status} body=${JSON.stringify(archiveResp.data)}`
                );
            }

            const sendResp = await api(
                page,
                csrf,
                `/api/chat/sessions/${sessionId}/messages`,
                'POST',
                { content: 'should not land', trigger_research: false }
            );
            if (sendResp.status !== 409) {
                throw new Error(
                    `Expected 409, got status=${sendResp.status} body=${JSON.stringify(sendResp.data)}`
                );
            }
            const errMsg = (sendResp.data?.error || '').toLowerCase();
            if (!errMsg.includes('archived')) {
                throw new Error(
                    `Expected error mentioning "archived", got: ${JSON.stringify(sendResp.data)}`
                );
            }
            console.log('PASSED');
            passed++;
        } catch (e) {
            console.log(`FAILED: ${e.message}`);
            await snap(page, 'reject');
            failed++;
        }

        // Test 2: Reactivate then send → expect 200.
        console.log('Test 2: reactivated session accepts send');
        try {
            const reactivateResp = await api(
                page,
                csrf,
                `/api/chat/sessions/${sessionId}`,
                'PATCH',
                { status: 'active' }
            );
            if (!reactivateResp.ok) {
                throw new Error(
                    `Reactivate PATCH failed: status=${reactivateResp.status} body=${JSON.stringify(reactivateResp.data)}`
                );
            }
            const sendResp = await api(
                page,
                csrf,
                `/api/chat/sessions/${sessionId}/messages`,
                'POST',
                { content: 'should land now', trigger_research: false }
            );
            if (!sendResp.ok || !sendResp.data?.success) {
                throw new Error(
                    `Expected success, got status=${sendResp.status} body=${JSON.stringify(sendResp.data)}`
                );
            }
            console.log('PASSED');
            passed++;
        } catch (e) {
            console.log(`FAILED: ${e.message}`);
            await snap(page, 'reactivate');
            failed++;
        }
    } catch (e) {
        console.log(`Test suite error: ${e.message}`);
        failed++;
    } finally {
        await browser.close();
    }

    console.log('-'.repeat(50));
    console.log(`Chat Archived-Session 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);
});
