import assert from 'node:assert/strict';
import fs from 'node:fs';
import path from 'node:path';
import http from 'node:http';
import { randomUUID } from 'node:crypto';
import { chromium, expect } from 'playwright/test';

const url = process.env.AGENT_WORKBENCH_URL || 'http://127.0.0.1:4319';
const caseRoot = path.resolve('.qa', 'ui-' + randomUUID());
const projectRoot = path.join(caseRoot, 'ui-project');
const secondaryRoot = path.join(caseRoot, 'other-project');
fs.mkdirSync(caseRoot, { recursive: true });
fs.cpSync('examples/starter-project', projectRoot, { recursive: true });
fs.mkdirSync(secondaryRoot);
fs.writeFileSync(path.join(secondaryRoot, 'SECONDARY.md'), '# Another project\n');
fs.mkdirSync('.qa/screenshots', { recursive: true });
const bootstrap = await (await fetch(url + '/api/bootstrap')).json();
assert.equal(bootstrap.config.mode, 'demo', 'UI validation requires an independent demo service.');
assert.equal(bootstrap.config.hasKey, false, 'UI validation must not replace a configured model connection.');
const api = async (route, input) => {
  const response = await fetch(url + '/api' + route, { method: input === undefined ? 'GET' : 'POST',
    headers: { 'Content-Type': 'application/json', 'X-Workbench-Token': bootstrap.token },
    ...(input === undefined ? {} : { body: JSON.stringify(input) }) });
  const result = await response.json();
  assert.ok(response.ok, result.error); return result;
};
const sleep = ms => new Promise(resolve => setTimeout(resolve, ms));
const passed = [], errors = [], networkErrors = [];
const mark = value => { passed.push(value); console.log('PASS ' + value); };
let stage = 'initial', page;
let modelRequests = 0;
const model = http.createServer(async (request, response) => {
  const chunks = []; for await (const chunk of request) chunks.push(chunk);
  response.writeHead(200, { 'Content-Type': 'application/json' });
  if (request.url === '/v1/models') return response.end(JSON.stringify({ data: [{ id: 'fixture-ui-agent' }] }));
  const input = JSON.parse(Buffer.concat(chunks).toString('utf8'));
  const lastUser = input.messages.findLastIndex(message => message.role === 'user');
  const count = input.messages.slice(lastUser + 1).filter(message => message.role === 'tool').length;
  modelRequests++;
  const tool = count === 0 ? { name: 'list_files', arguments: JSON.stringify({ path: '', depth: 2 }) }
    : count === 1 ? { name: 'propose_file', arguments: JSON.stringify({ path: 'COMPATIBLE-TEST.md', content: '# 本地兼容协议验证\n\n本文件来自受控接口测试，没有使用真实大模型。\n', reason: '浏览器端到端兼容协议测试' }) } : null;
  response.end(JSON.stringify({ choices: [{ message: { role: 'assistant', content: tool ? '本地兼容接口测试。' : '兼容协议流程已完成，本次没有使用真实大模型。',
    ...(tool ? { tool_calls: [{ id: 'ui-' + randomUUID(), type: 'function', function: tool }] } : {}) }, finish_reason: tool ? 'tool_calls' : 'stop' }], usage: { prompt_tokens: 99, completion_tokens: 30 } }));
});
await new Promise(resolve => model.listen(0, '127.0.0.1', resolve));
const browser = await chromium.launch({ channel: 'msedge', headless: true });
const openProject = async root => {
  await page.getByRole('button', { name: '打开项目', exact: true }).click();
  const dialog = page.getByRole('dialog', { name: '打开项目' });
  await dialog.getByLabel('项目文件夹路径').fill(root);
  await dialog.getByRole('button', { name: '打开项目', exact: true }).click();
  await expect(dialog).not.toBeVisible();
};
const assertViewport = async () => {
  const bounds = await page.evaluate(() => {
    const header = document.querySelector('.work-header').getBoundingClientRect();
    const composer = document.querySelector('.composer-wrap').getBoundingClientRect();
    return { scrollY: window.scrollY, top: header.top, bottom: composer.bottom, height: window.innerHeight,
      scrollHeight: document.documentElement.scrollHeight, scrollWidth: document.documentElement.scrollWidth, width: window.innerWidth };
  });
  assert.equal(bounds.scrollY, 0, 'The document should not scroll');
  assert.ok(bounds.top >= -1 && bounds.bottom <= bounds.height + 1, 'Header and composer must stay in the viewport: ' + JSON.stringify(bounds));
  assert.ok(bounds.scrollHeight <= bounds.height + 1 && bounds.scrollWidth <= bounds.width + 1, 'The page should not overflow');
};
const screenshot = async name => {
  const toastClose = page.locator('.toast').getByRole('button', { name: '关闭提示', exact: true });
  if (await toastClose.isVisible()) await toastClose.click();
  await assertViewport();
  return page.screenshot({ path: '.qa/screenshots/' + name + '.png', fullPage: true });
};
const newTask = async content => {
  await page.getByRole('button', { name: '新建任务', exact: true }).click();
  await page.getByLabel('任务内容').fill(content);
  await page.getByRole('button', { name: '发送任务', exact: true }).click();
};
try {
  page = await browser.newPage({ viewport: { width: 1440, height: 960 }, deviceScaleFactor: 1, acceptDownloads: true });
  page.setDefaultTimeout(10000);
  page.on('pageerror', error => errors.push(error.message));
  page.on('requestfailed', request => { if (!request.url().includes('/api/events')) networkErrors.push(request.url() + ': ' + request.failure()?.errorText); });
  await page.goto(url, { waitUntil: 'domcontentloaded' });
  await page.waitForLoadState('networkidle', { timeout: 6000 }).catch(() => {});
  await page.locator('.connection.connected').waitFor();
  stage = 'open project'; await openProject(projectRoot);
  await expect(page.locator('.tree-row[title="src/catalog.js"]')).toBeVisible();
  const selectedProject = (await api('/bootstrap')).projects.find(project => project.root.toLowerCase() === projectRoot.toLowerCase());
  assert.ok(selectedProject);
  await screenshot('desktop-ready');
  stage = 'file preview'; await page.locator('.tree-row[title="README.md"]').click();
  await expect(page.locator('.file-heading > span')).toHaveText('README.md');
  await expect(page.locator('.code-view')).toContainText('研究任务看板');
  mark('project selection and real file preview');

  stage = 'offline task'; await newTask('UI 验证：读取项目并提出改动');
  await page.locator('.approval-card').waitFor();
  await expect(page.locator('.approval-card > pre')).toHaveText('node --version');
  assert.equal(fs.existsSync(path.join(projectRoot, 'WORKBENCH-NOTES.md')), false);
  await page.locator('.inspector-tabs').getByRole('button', { name: /^改动/ }).click();
  await expect(page.locator('.diff-view')).toContainText('工作台验证记录');
  await screenshot('command-approval');
  stage = 'approval delayed snapshot';
  const approvalRoute = '**/api/sessions/*/approvals/*';
  await page.route(approvalRoute, async route => { const response = await route.fetch(); await sleep(700); await route.fulfill({ response }); });
  await page.getByRole('button', { name: '确认执行', exact: true }).click();
  await expect(page.locator('.task-toolbar .status')).toHaveText('待审查');
  await sleep(800); await expect(page.locator('.task-toolbar .status')).toHaveText('待审查');
  await page.unroute(approvalRoute);
  await page.locator('.inline-event .tool-row').filter({ hasText: 'node --version' }).click();
  await expect(page.locator('.event-details')).toContainText(/v\d+\.\d+\.\d+/);
  mark('real command consent, execution and stale snapshot rejection');
  stage = 'apply proposal'; await page.locator('.review-callout').click();
  await screenshot('desktop-diff-review');
  await page.getByRole('button', { name: '应用这项改动', exact: true }).click();
  await expect(page.locator('.change-summary .status')).toHaveText('已应用');
  await expect(page.locator('.task-toolbar .status')).toHaveText('已完成');
  assert.match(fs.readFileSync(path.join(projectRoot, 'WORKBENCH-NOTES.md'), 'utf8'), /没有调用大模型/);
  mark('diff review and verified file application');

  stage = 'manual edit'; await page.locator('.tree-row[title="README.md"]').click();
  await page.getByRole('button', { name: '编辑文件', exact: true }).click();
  const original = await page.getByLabel('编辑文件内容').inputValue();
  await page.getByLabel('编辑文件内容').fill(original + '\n## UI 验证\n手动编辑已通过差异审查。\n');
  await page.getByRole('button', { name: '保存为提案', exact: true }).click();
  await expect(page.locator('.task-toolbar .status')).toHaveText('待审查');
  assert.ok(!fs.readFileSync(path.join(projectRoot, 'README.md'), 'utf8').includes('手动编辑已通过'));
  await page.getByRole('button', { name: '应用这项改动', exact: true }).click();
  await expect(page.locator('.change-summary .status')).toHaveText('已应用');
  assert.match(fs.readFileSync(path.join(projectRoot, 'README.md'), 'utf8'), /手动编辑已通过/);
  mark('manual editor creates a reviewable proposal');

  stage = 'file response ordering';
  const delayedFile = '**/api/projects/' + selectedProject.id + '/file?path=README.md';
  await page.route(delayedFile, async route => { const response = await route.fetch(); await sleep(450); await route.fulfill({ response }); });
  await page.locator('.tree-row[title="README.md"]').click();
  await page.locator('.tree-row[title="src/catalog.js"]').click();
  await expect(page.locator('.file-heading > span')).toHaveText('src/catalog.js');
  await sleep(550); await expect(page.locator('.file-heading > span')).toHaveText('src/catalog.js');
  await page.unroute(delayedFile); mark('file responses stay with the current selection');

  stage = 'session persistence and ordering'; await newTask('UI 第二任务：拒绝演示命令');
  await page.locator('.approval-card').waitFor();
  await page.getByRole('button', { name: '拒绝执行', exact: true }).click();
  await expect(page.locator('.task-toolbar .status')).toHaveText('待审查');
  const sessions = await api('/sessions');
  const first = sessions.find(session => session.title.startsWith('UI 验证：'));
  const second = sessions.find(session => session.title.startsWith('UI 第二任务：'));
  assert.ok(first && second);
  const delayedSession = '**/api/sessions/' + first.id;
  await page.route(delayedSession, async route => { const response = await route.fetch(); await sleep(450); await route.fulfill({ response }); });
  await page.locator('.session-select').filter({ hasText: 'UI 验证：' }).click();
  await page.locator('.session-select').filter({ hasText: 'UI 第二任务：' }).click();
  await expect(page.locator('.work-title h1')).toHaveText(second.title);
  await sleep(550); await expect(page.locator('.work-title h1')).toHaveText(second.title);
  await page.unroute(delayedSession);
  await page.reload({ waitUntil: 'domcontentloaded' });
  await expect(page.locator('.work-title h1')).toHaveText(second.title);
  const downloadPromise = page.waitForEvent('download');
  await page.getByRole('button', { name: '导出会话', exact: true }).click();
  const download = await downloadPromise; await download.saveAs(path.join(caseRoot, 'session-export.json'));
  assert.equal(JSON.parse(fs.readFileSync(path.join(caseRoot, 'session-export.json'), 'utf8')).session.id, second.id);
  mark('denial, session selection, reload, stale response rejection and export');

  stage = 'project response ordering'; await openProject(secondaryRoot);
  await expect(page.locator('.tree-row[title="SECONDARY.md"]')).toBeVisible();
  const otherProject = (await api('/bootstrap')).projects.find(project => project.root.toLowerCase() === secondaryRoot.toLowerCase());
  const delayedInventory = '**/api/projects/' + selectedProject.id + '/files';
  await page.route(delayedInventory, async route => { const response = await route.fetch(); await sleep(450); await route.fulfill({ response }); });
  await page.getByLabel('当前项目').selectOption(selectedProject.id);
  await page.getByLabel('当前项目').selectOption(otherProject.id);
  await expect(page.locator('.tree-row[title="SECONDARY.md"]')).toBeVisible();
  await sleep(550); await expect(page.locator('.tree-row[title="src/catalog.js"]')).toHaveCount(0);
  await page.unroute(delayedInventory);
  await page.getByLabel('当前项目').selectOption(selectedProject.id);
  await expect(page.locator('.tree-row[title="src/catalog.js"]')).toBeVisible();
  mark('project inventory responses cannot replace another project');

  stage = 'narrow and mobile'; await page.setViewportSize({ width: 780, height: 960 });
  await page.getByRole('button', { name: '查看改动', exact: true }).click();
  await expect(page.locator('.inspector')).toBeVisible();
  await screenshot('tablet-diff');
  await page.getByRole('button', { name: '关闭检视区', exact: true }).click();
  await page.setViewportSize({ width: 390, height: 844 });
  await expect(page.locator('.workbench')).toHaveClass(/sidebar-hidden/);
  await expect(page.locator('.project-sidebar')).not.toBeVisible();
  await expect(page.getByRole('button', { name: '展开项目栏', exact: true })).toBeVisible();
  await page.getByRole('button', { name: '展开项目栏', exact: true }).click();
  await expect(page.locator('.project-sidebar')).toBeVisible();
  await page.locator('.tree-row[title="README.md"]').click();
  await expect(page.locator('.inspector')).toBeVisible();
  await expect(page.locator('.file-heading > span')).toHaveText('README.md');
  await screenshot('mobile-file');
  await page.getByRole('button', { name: '关闭检视区', exact: true }).click();
  await page.getByRole('button', { name: '查看执行轨迹', exact: true }).click();
  await expect(page.locator('.trace-list')).toBeVisible();
  await screenshot('mobile-trace');
  await page.getByRole('button', { name: '关闭检视区', exact: true }).click();
  assert.ok(await page.evaluate(() => document.documentElement.scrollWidth <= window.innerWidth));
  await screenshot('mobile-conversation');
  mark('780 and 390 px: project drawer, file, diff and trace controls, no page overflow');

  stage = 'research'; await page.setViewportSize({ width: 1440, height: 960 });
  if (!(await page.locator('.project-sidebar').isVisible())) await page.getByRole('button', { name: '展开项目栏', exact: true }).click();
  await page.getByRole('button', { name: '机制研究与设计', exact: true }).click();
  const research = page.getByRole('dialog', { name: '机制研究与独立设计' });
  await expect(research.locator('.research-content')).toContainText('统一机制报告');
  assert.ok(await research.locator('table').count() >= 2);
  await screenshot('research-overview');
  await research.locator('nav').getByRole('button', { name: /Claude Code：工具循环/ }).click();
  await expect(research.locator('.research-content')).toContainText('工具调度、权限与结果机制');
  await research.locator('nav').getByRole('button', { name: /Claude Code：上下文与恢复/ }).click();
  await expect(research.locator('.research-content')).toContainText('上下文压缩与会话恢复');
  await research.locator('nav').getByRole('button', { name: /Cursor：执行与编辑/ }).click();
  await expect(research.locator('.research-content')).toContainText('本地工具执行与恢复机制');
  await research.getByRole('button', { name: '关闭窗口', exact: true }).click();
  mark('unified and detailed research reports render');

  stage = 'compatible model settings';
  const openLegacy = async () => {
    await page.getByRole('button', { name: '选择与管理模型', exact: true }).click();
    await page.getByRole('dialog', { name: '模型中心', exact: true }).locator('.mc-connection').filter({ has: page.getByRole('heading', { name: '原模型连接', exact: true }) }).getByRole('button', { name: '编辑', exact: true }).click();
  };
  await openLegacy();
  const settings = page.getByRole('dialog', { name: '原连接与演示设置', exact: true });
  await settings.getByRole('button', { name: /兼容模型服务/ }).click();
  await settings.getByLabel('接口根地址', { exact: true }).fill('http://127.0.0.1:' + model.address().port + '/v1');
  await settings.getByLabel('API Key（按服务要求填写）', { exact: true }).fill('fixture-ui-key');
  await settings.getByRole('button', { name: '读取模型列表', exact: true }).click();
  await expect(settings.getByLabel('模型名称', { exact: true })).toHaveValue('fixture-ui-agent');
  assert.equal((await api('/bootstrap')).config.hasKey, false);
  await settings.getByRole('button', { name: '保存并切换原连接', exact: true }).click();
  await expect(page.getByRole('button', { name: '切换当前模型', exact: true })).toContainText('fixture-ui-agent');
  await newTask('UI 兼容接口验证');
  await expect(page.locator('.conversation')).toContainText('兼容协议流程已完成', { timeout: 20000 });
  await expect(page.locator('.task-toolbar .status')).toHaveText('待审查');
  assert.equal(modelRequests, 3);
  await page.locator('.review-callout').click();
  await page.getByRole('button', { name: '应用这项改动', exact: true }).click();
  await expect(page.locator('.change-summary .status')).toHaveText('已应用');
  assert.match(fs.readFileSync(path.join(projectRoot, 'COMPATIBLE-TEST.md'), 'utf8'), /没有使用真实大模型/);
  await screenshot('compatible-fixture');
  await openLegacy();
  await settings.getByRole('button', { name: '清除 Key', exact: true }).click();
  await settings.getByRole('button', { name: /离线演示/ }).click();
  await settings.getByRole('button', { name: '保存并切换原连接', exact: true }).click();
  assert.equal((await api('/bootstrap')).config.hasKey, false);
  mark('model list, ephemeral key, compatible fixture tool loop and return to offline mode');

  stage = 'complete'; assert.deepEqual(errors, []); assert.deepEqual(networkErrors, []);
  const summary = { success: true, url, browser: 'Microsoft Edge ' + browser.version(), date: new Date().toISOString(), projectRoot,
    viewports: [1440, 780, 390], passed, pageErrors: errors, requestFailures: networkErrors, modelValidation: 'controlled local HTTP fixture; no real model' };
  fs.writeFileSync('.qa/ui-validation.json', JSON.stringify(summary, null, 2));
  console.log(JSON.stringify(summary, null, 2));
} catch (error) {
  if (page) { await screenshot('failure-' + stage.replace(/[^a-z0-9]+/gi, '-')).catch(() => {}); fs.writeFileSync('.qa/failure-dom.html', await page.content().catch(() => '')); }
  fs.writeFileSync('.qa/ui-validation.json', JSON.stringify({ success: false, stage, error: error.message, passed, pageErrors: errors }, null, 2));
  throw error;
} finally {
  await browser.close();
  await api('/config', { ...bootstrap.config, mode: 'demo', apiKey: '' }).catch(() => {});
  model.closeAllConnections(); await new Promise(resolve => model.close(resolve));
}
