#!/usr/bin/env node
"use strict";

const assert = require("node:assert");
const fs = require("node:fs");
const path = require("node:path");
const { chromium } = require("playwright");

const BASE_URL = (process.env.MNOTE_WEB_SMOKE_BASE_URL || "http://127.0.0.1:3000").replace(/\/+$/, "");
const ROOT_URI = process.env.MNOTE_KNOWLEDGE_RAG_ROOT_URI || "file:///mnt/Data1T/Mnote_data/users/mnote-e2e/workspaces/my-space";
const WORKSPACE_ID = process.env.MNOTE_KNOWLEDGE_RAG_WORKSPACE_ID || "local-ws:mnote-e2e:my-space";
const GROUP_QUERY = process.env.MNOTE_KNOWLEDGE_RAG_GROUP_QUERY || "三甲基硅";
const SHORT_QUERY = process.env.MNOTE_KNOWLEDGE_RAG_SHORT_QUERY || "吗啉";
const TOO_SHORT_QUERY = process.env.MNOTE_KNOWLEDGE_RAG_TOO_SHORT_QUERY || "吗";
const OUTPUT_DIR = path.join(process.cwd(), "tmp", "task542-knowledge-rag-search-grouping-and-short-query-smoke");
const RESULT_PATH = path.join(OUTPUT_DIR, "result.json");
const GROUP_SCREENSHOT_PATH = path.join(OUTPUT_DIR, "grouped-collapsed.png");
const EXPANDED_SCREENSHOT_PATH = path.join(OUTPUT_DIR, "grouped-expanded.png");
const UI_TIMEOUT_MS = Number(process.env.MNOTE_SMOKE_UI_TIMEOUT_MS || 30_000);
const CHROMIUM_EXECUTABLE_PATH = process.env.PLAYWRIGHT_CHROMIUM_EXECUTABLE_PATH
  || ["/usr/bin/google-chrome-stable", "/usr/bin/google-chrome", "/snap/bin/chromium"]
    .find((candidate) => fs.existsSync(candidate));

async function main() {
  fs.mkdirSync(OUTPUT_DIR, { recursive: true });
  const browser = await chromium.launch({ headless: true, executablePath: CHROMIUM_EXECUTABLE_PATH });
  const context = await browser.newContext({ viewport: { width: 1360, height: 900 } });
  try {
    const auth = await context.request.post(`${BASE_URL}/api/auth`, {
      data: {
        action: "auth:signIn",
        args: {
          provider: "password",
          params: {
            email: "mnote.e2e@example.com",
            password: process.env.MNOTE_E2E_PASSWORD || "MnoteE2E123!",
            flow: "signIn",
          },
        },
      },
    });
    assert(auth.ok(), `登录失败: ${auth.status()} ${await auth.text()}`);

    const localSearch = await context.request.post(`${BASE_URL}/api/search/documents`, {
      data: {
        workspaceId: WORKSPACE_ID,
        sourceKind: "local_folder",
        rootUri: ROOT_URI,
        query: SHORT_QUERY,
        limit: 30,
        filters: {
          includeOcr: false,
          titleOnly: false,
          exact: false,
          onlyCurrentPage: false,
          timeRange: "any",
          timeField: "updated",
        },
      },
    });
    assert(localSearch.ok(), `本地搜索失败: ${localSearch.status()} ${await localSearch.text()}`);
    const localPayload = await localSearch.json();
    assert.equal(localPayload.results?.length || 0, 0, `本地文档搜索不应把两字 CJK query 匹配到无关文档: ${JSON.stringify(localPayload.results?.slice(0, 3), null, 2)}`);

    const knowledgeSearch = await context.request.post(`${BASE_URL}/api/knowledge-rag/search`, {
      data: {
        workspaceId: WORKSPACE_ID,
        rootUri: ROOT_URI,
        query: SHORT_QUERY,
        mode: "mix",
        topK: 12,
        chunkTopK: 24,
        includeChunkContent: true,
      },
    });
    assert(knowledgeSearch.ok(), `资料库短 query 检索失败: ${knowledgeSearch.status()} ${await knowledgeSearch.text()}`);
    const knowledgePayload = await knowledgeSearch.json();
    assert((knowledgePayload.results?.length || 0) >= 8, `资料库 2 字 query 应返回段落去重后的 LightRAG 检索命中: ${JSON.stringify(knowledgePayload, null, 2).slice(0, 2000)}`);
    assert(String(knowledgePayload.results?.[0]?.snippet || "").includes(SHORT_QUERY), `资料库结果未包含 ${SHORT_QUERY}: ${JSON.stringify(knowledgePayload.results?.[0], null, 2)}`);
    assert(knowledgePayload.references?.some((reference) => reference.matchSource === "lightrag_search"), `2 字 query 应走 LightRAG search provider: ${JSON.stringify(knowledgePayload.references?.slice(0, 3), null, 2)}`);
    const firstKnowledgeResult = knowledgePayload.results?.[0] || {};
    const firstKnowledgeCitation = knowledgePayload.citations?.[0] || {};
    assert(firstKnowledgeResult.displayQuote && firstKnowledgeResult.locatorEvidenceText, `搜索结果缺少 displayQuote/locatorEvidenceText: ${JSON.stringify(firstKnowledgeResult, null, 2)}`);
    assert(firstKnowledgeCitation.citationId && firstKnowledgeCitation.displayQuote && firstKnowledgeCitation.locatorEvidenceText, `citations[] 缺少统一引用字段: ${JSON.stringify(firstKnowledgeCitation, null, 2)}`);
    assert(
      !/(?:<\/?e(?:q(?:uation)?)?\b|<\/?drawing\b|format=["']?latex|\blatex\b)/i.test(String(firstKnowledgeResult.displayQuote || "")),
      `displayQuote 仍暴露原始公式/绘图标记: ${firstKnowledgeResult.displayQuote}`
    );

    const tooShortSearch = await context.request.post(`${BASE_URL}/api/knowledge-rag/search`, {
      data: {
        workspaceId: WORKSPACE_ID,
        rootUri: ROOT_URI,
        query: TOO_SHORT_QUERY,
        mode: "mix",
        topK: 12,
        chunkTopK: 24,
        includeChunkContent: true,
      },
    });
    assert.equal(tooShortSearch.status(), 400, `资料库 1 字 query 应返回 400: ${tooShortSearch.status()} ${await tooShortSearch.text()}`);
    const tooShortPayload = await tooShortSearch.json();
    assert.equal(tooShortPayload.code, "knowledge_rag_search_query_too_short", `1 字 query 错误码不符合预期: ${JSON.stringify(tooShortPayload)}`);
    assert(String(tooShortPayload.message || "").includes("至少 2 个字"), `1 字 query 提示不符合预期: ${JSON.stringify(tooShortPayload)}`);

    const page = await context.newPage();
    const pageErrors = [];
    const consoleErrors = [];
    page.on("pageerror", (error) => pageErrors.push(String(error?.stack || error?.message || error)));
    page.on("console", (message) => {
      if (["error", "warning"].includes(message.type())) consoleErrors.push(`${message.type()}: ${message.text()}`);
    });
    await page.goto(`${BASE_URL}/`, { waitUntil: "domcontentloaded", timeout: UI_TIMEOUT_MS });
    await page.evaluate(() => {
      window.localStorage?.setItem("mnote.search.collapseSourcesDefault.v1", "1");
    });
    await page.waitForSelector('[data-mnote-action="open-search-modal"]', { timeout: UI_TIMEOUT_MS });
    await page.click('[data-mnote-action="open-search-modal"]');
    await page.waitForSelector('[data-testid="wolai-search-input"]', { timeout: UI_TIMEOUT_MS });
    await page.evaluate(() => {
      const knowledge = document.querySelector('[data-search-switch="knowledge"]');
      if (knowledge instanceof HTMLElement) {
        knowledge.setAttribute("aria-checked", "true");
        knowledge.classList.add("is-on");
      }
      const collapse = document.querySelector('[data-search-switch="collapseSource"]');
      if (collapse instanceof HTMLElement) {
        collapse.setAttribute("aria-checked", "true");
        collapse.classList.add("is-on");
      }
    });
    await page.fill('[data-testid="wolai-search-input"]', TOO_SHORT_QUERY);
    await page.evaluate(() => {
      document.querySelector('[data-testid="wolai-search-input"]')
        ?.dispatchEvent(new Event("input", { bubbles: true }));
    });
    await page.waitForSelector('[data-search-empty="true"]', { timeout: UI_TIMEOUT_MS });
    const tooShortUi = await page.evaluate(() => ({
      meta: document.querySelector('[data-testid="wolai-search-result-meta"]')?.textContent || "",
      empty: document.querySelector('[data-search-empty="true"]')?.textContent || "",
      rows: document.querySelectorAll('[data-testid="wolai-search-result-row"]').length,
    }));
    assert(tooShortUi.empty.includes("至少 2 个字"), `1 字 query UI 未提示至少 2 个字: ${JSON.stringify(tooShortUi)}`);
    assert.equal(tooShortUi.rows, 0, `1 字 query 不应发起并渲染结果: ${JSON.stringify(tooShortUi)}`);

    await page.fill('[data-testid="wolai-search-input"]', GROUP_QUERY);
    await page.evaluate(() => {
      document.querySelector('[data-testid="wolai-search-input"]')
        ?.dispatchEvent(new Event("input", { bubbles: true }));
    });
    await page.waitForSelector(".wolai-search-source-group", { timeout: UI_TIMEOUT_MS });
    const collapsed = await page.evaluate(() => ({
      switchOn: document.querySelector('[data-search-switch="collapseSource"]')?.getAttribute("aria-checked") === "true",
      groups: document.querySelectorAll(".wolai-search-source-group").length,
      visibleRows: Array.from(document.querySelectorAll('[data-testid="wolai-search-result-row"]'))
        .filter((row) => row.offsetParent !== null).length,
      meta: document.querySelector('[data-testid="wolai-search-result-meta"]')?.textContent || "",
      firstHeader: document.querySelector(".wolai-search-source-header")?.textContent || "",
    }));
    await page.screenshot({ path: GROUP_SCREENSHOT_PATH, fullPage: true });
    assert.equal(collapsed.switchOn, true, `折叠同来源开关未默认打开: ${JSON.stringify(collapsed)}`);
    assert(collapsed.groups >= 1, `未按来源分组: ${JSON.stringify(collapsed)}`);
    assert.equal(collapsed.visibleRows, 0, `默认折叠时不应展示重复来源子结果: ${JSON.stringify(collapsed)}`);
    assert(
      !String(collapsed.firstHeader || "").includes("有机合成中的保护基/[OCR]"),
      `来源组标题不应再在标题下重复显示完整来源路径: ${JSON.stringify(collapsed)}`
    );

    await page.click(".wolai-search-source-header");
    await page.waitForFunction(() => {
      return Array.from(document.querySelectorAll('[data-testid="wolai-search-result-row"]'))
        .some((row) => row.offsetParent !== null);
    }, null, { timeout: UI_TIMEOUT_MS });
    const expanded = await page.evaluate(() => ({
      expanded: document.querySelector(".wolai-search-source-header")?.getAttribute("aria-expanded") === "true",
      visibleRows: Array.from(document.querySelectorAll('[data-testid="wolai-search-result-row"]'))
        .filter((row) => row.offsetParent !== null).length,
      firstRows: Array.from(document.querySelectorAll('[data-testid="wolai-search-result-row"]'))
        .slice(0, 3)
        .map((row) => row.textContent),
    }));
    await page.screenshot({ path: EXPANDED_SCREENSHOT_PATH, fullPage: true });
    assert.equal(expanded.expanded, true, `来源组未展开: ${JSON.stringify(expanded)}`);
    assert(expanded.visibleRows > 0, `展开后未展示来源下结果: ${JSON.stringify(expanded)}`);
    assert(
      expanded.firstRows.every((text) => !String(text || "").includes("有机合成中的保护基/[OCR]")),
      `折叠同来源展开后，子结果不应重复显示完整来源路径: ${JSON.stringify(expanded.firstRows)}`
    );
    assert(
      expanded.firstRows.every((text) => !String(text || "").trim().endsWith("docx")),
      `折叠同来源展开后，子结果不应重复显示来源类型: ${JSON.stringify(expanded.firstRows)}`
    );
    assert(
      expanded.firstRows.every((text) => !/(?:<\/?e(?:q(?:uation)?)?\b|<\/?drawing\b|format=["']?latex|\blatex\b)/i.test(String(text || ""))),
      `资料库搜索结果仍暴露原始公式/绘图标记: ${JSON.stringify(expanded.firstRows)}`
    );

    const result = {
      ok: true,
      baseUrl: BASE_URL,
      rootUri: ROOT_URI,
      workspaceId: WORKSPACE_ID,
      shortQuery: SHORT_QUERY,
      tooShortQuery: TOO_SHORT_QUERY,
      groupQuery: GROUP_QUERY,
      localCount: localPayload.results?.length || 0,
      knowledgeCount: knowledgePayload.results?.length || 0,
      knowledgeFirst: {
        title: knowledgePayload.results?.[0]?.title,
        snippet: knowledgePayload.results?.[0]?.snippet,
        blockId: knowledgePayload.results?.[0]?.locator?.blockId,
      },
      tooShortUi,
      collapsed,
      expanded,
      pageErrors,
      consoleErrors,
      screenshots: {
        collapsed: GROUP_SCREENSHOT_PATH,
        expanded: EXPANDED_SCREENSHOT_PATH,
      },
    };
    fs.writeFileSync(RESULT_PATH, `${JSON.stringify(result, null, 2)}\n`, "utf8");
    console.log(JSON.stringify(result, null, 2));
  } finally {
    await browser.close().catch(() => undefined);
  }
}

main().catch((error) => {
  fs.mkdirSync(OUTPUT_DIR, { recursive: true });
  fs.writeFileSync(
    path.join(OUTPUT_DIR, "failure.json"),
    `${JSON.stringify({ ok: false, error: error.stack || error.message || String(error) }, null, 2)}\n`,
    "utf8",
  );
  console.error(error.stack || error.message || String(error));
  process.exit(1);
});
