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

const assert = require("node:assert");
const fs = require("node:fs");
const os = require("node:os");
const path = require("node:path");
const { execFileSync } = require("node:child_process");
const { chromium } = require("playwright");
const {
  BASE_URL,
  UI_TIMEOUT_MS,
  ensureAuthenticated,
} = require("./tree-shell-smoke-helpers");

const CONTROL_PLANE_DB = "/mnt/Data1T/Mnote_data/control-plane/control-plane.db";
const CHROMIUM_EXECUTABLE_PATH =
  process.env.PLAYWRIGHT_CHROMIUM_EXECUTABLE_PATH ||
  [
    "/usr/bin/google-chrome-stable",
    "/usr/bin/google-chrome",
    "/snap/bin/chromium",
    "/usr/bin/chromium",
  ].find((candidate) => fs.existsSync(candidate));

const PROVIDERS = [
  {
    key: "gpt",
    profileId: "shared_api_gpt_chat",
    chipText: "ChatOnly / GPT",
    markerPrefix: "MNOTE_API_CHAT_GPT",
    expectedProfile: "api-gpt-chat",
    expectedModel: "aisz-chat/gpt-5.5-extra-high-fast",
  },
  {
    key: "deepseek-flash",
    profileId: "shared_api_deepseek_flash_chat",
    chipText: "ChatOnly / DeepSeek Flash",
    markerPrefix: "MNOTE_API_CHAT_DEEPSEEK_FLASH",
    expectedProfile: "api-deepseek-flash-chat",
    expectedModel: "deepseek-v4-flash",
  },
];

const OUTPUT_DIR = path.join(process.cwd(), "tmp", "task527-chatonly-api-provider-smoke");
const RESULT_PATH = path.join(OUTPUT_DIR, "result.json");

function fileUrl(localPath) {
  return `file://${localPath}`;
}

function localMdDocumentId(relativePath) {
  return `local-md:${relativePath.replaceAll("/", "~2F")}`;
}

function documentUrl(root, relativePath) {
  const url = new URL(`${BASE_URL}/documents/${encodeURIComponent(localMdDocumentId(relativePath))}`);
  url.searchParams.set("sourceKind", "local_folder");
  url.searchParams.set("rootUri", fileUrl(root));
  url.searchParams.set("treeView", "filetree");
  return url.toString();
}

function writeWorkspaceManifest(root, ownerId, workspaceId) {
  fs.mkdirSync(path.join(root, ".mnote"), { recursive: true });
  fs.writeFileSync(
    path.join(root, ".mnote", "workspace.json"),
    `${JSON.stringify(
      {
        workspaceId,
        ownerId,
        createdAt: new Date().toISOString(),
        capabilities: ["local_files", "ai_sessions", "markdown_edit"],
      },
      null,
      2,
    )}\n`,
    "utf8",
  );
}

function sqlQuote(value) {
  return `'${String(value).replaceAll("'", "''")}'`;
}

function sqliteExec(sql) {
  execFileSync("sqlite3", [CONTROL_PLANE_DB, sql], {
    encoding: "utf8",
    stdio: ["ignore", "pipe", "pipe"],
  });
}

function sqliteJson(sql, fallback = null) {
  try {
    const raw = execFileSync("sqlite3", ["-json", CONTROL_PLANE_DB, sql], {
      encoding: "utf8",
      stdio: ["ignore", "pipe", "pipe"],
    }).trim();
    return raw ? JSON.parse(raw) : fallback;
  } catch {
    return fallback;
  }
}

function grantWorkspaceAccess({ actorId, workspaceId, root, rootUri, grantId }) {
  const now = new Date().toISOString();
  sqliteExec(`
    INSERT OR IGNORE INTO users (id, email, username, display_name, role, status, created_at, updated_at, revision)
    VALUES (${sqlQuote(actorId)}, ${sqlQuote(`${actorId}@example.com`)}, ${sqlQuote(actorId)}, ${sqlQuote(actorId)}, 'user', 'active', ${sqlQuote(now)}, ${sqlQuote(now)}, 1);
    INSERT OR REPLACE INTO workspaces (id, owner_user_id, name, kind, root_uri, root_path, source_kind, status, created_at, updated_at, revision)
    VALUES (${sqlQuote(workspaceId)}, ${sqlQuote(actorId)}, ${sqlQuote(actorId)}, 'personal', ${sqlQuote(rootUri)}, ${sqlQuote(root)}, 'local_folder', 'active', ${sqlQuote(now)}, ${sqlQuote(now)}, 1);
    INSERT OR REPLACE INTO directory_grants (id, user_id, workspace_id, root_uri, root_path, permission, recursive, capabilities_json, source, status, created_by, created_at, updated_at, revision)
    VALUES (${sqlQuote(grantId)}, ${sqlQuote(actorId)}, ${sqlQuote(workspaceId)}, ${sqlQuote(rootUri)}, ${sqlQuote(root)}, 'write', 1, '["ai","markdown_edit"]', 'smoke', 'active', ${sqlQuote(actorId)}, ${sqlQuote(now)}, ${sqlQuote(now)}, 1);
  `);
}

async function saveScreenshot(page, name) {
  const target = path.join(OUTPUT_DIR, `${name}.png`);
  await page.screenshot({ path: target, fullPage: false });
  return target;
}

async function waitForAssistantMarker(page, marker) {
  await page.waitForFunction(
    (expectedMarker) => {
      const assistantText = Array.from(document.querySelectorAll(".wolai-page-ai-message--assistant"))
        .map((node) => node.textContent || "")
        .join("\n");
      return assistantText.includes(expectedMarker);
    },
    marker,
    { timeout: 180_000 },
  );
}

async function ensurePageAiDrawerOpen(page) {
  const drawer = page.locator('[data-testid="wolai-page-ai-drawer"]');
  if (await drawer.isVisible().catch(() => false)) return;
  await page.getByTestId("wolai-floating-ai").click({ timeout: UI_TIMEOUT_MS });
  await drawer.waitFor({ state: "visible", timeout: UI_TIMEOUT_MS });
}

async function selectProvider(page, provider) {
  await page.locator("[data-page-ai-agent-button]").click({ timeout: UI_TIMEOUT_MS });
  await page.locator(`[data-page-ai-agent-id="chat_only"][data-page-ai-profile-id="${provider.profileId}"]`).click({
    timeout: UI_TIMEOUT_MS,
  });
  await page.waitForFunction(
    (expected) => document.querySelector("[data-page-ai-agent-chip]")?.textContent?.includes(expected),
    provider.chipText,
    { timeout: UI_TIMEOUT_MS },
  );
}

async function runProviderSmoke(page, provider, suffix) {
  const marker = `${provider.markerPrefix}_${suffix}`;
  const runRequests = [];
  const runResponses = [];
  const deleteResponses = [];

  const runRoute = async (route) => {
    runRequests.push(JSON.parse(route.request().postData() || "{}"));
    await route.continue();
  };
  await page.route("**/api/hermes/client/runs", runRoute);
  const responseListener = async (response) => {
    const url = response.url();
    const request = response.request();
    if (request.method() === "POST" && url.includes("/api/hermes/client/runs")) {
      runResponses.push({
        url,
        status: response.status(),
        body: await response.text().catch(() => ""),
      });
    }
    if (request.method() === "DELETE" && url.includes("/api/hermes/client/sessions/")) {
      deleteResponses.push({
        url,
        status: response.status(),
        body: await response.text().catch(() => ""),
      });
    }
  };
  page.on("response", responseListener);

  try {
    await page.locator('.wolai-page-ai-header-actions [data-page-ai-action="new-session"]').click({
      timeout: UI_TIMEOUT_MS,
    });
    await selectProvider(page, provider);

    await page.locator("[data-page-ai-input]").fill(`请只回复：${marker}`, { timeout: UI_TIMEOUT_MS });
    await page.locator('[data-page-ai-action="send"]').click({ timeout: UI_TIMEOUT_MS });
    await waitForAssistantMarker(page, marker);
    await page.waitForFunction(
      () => (document.querySelector("[data-page-ai-run-status]")?.textContent || "").includes("完成"),
      null,
      { timeout: 180_000 },
    ).catch(() => {});
    const afterMessageScreenshot = await saveScreenshot(page, `${provider.key}-after-message`);

    assert.strictEqual(runRequests.length, 1, `${provider.key} 本轮应只创建一个 run`);
    const run = runRequests[0];
    assert.strictEqual(run.agentId, "chat_only", `${provider.key} 应使用 ChatOnly agent`);
    assert.strictEqual(run.profileId, provider.profileId, `${provider.key} 应使用 API ChatOnly profile`);
    assert(run.sessionId, `${provider.key} run payload 应包含 MNote sessionId`);

    assert.strictEqual(runResponses.length, 1, `${provider.key} 应返回一个 run response`);
    assert.strictEqual(runResponses[0].status, 200, `${provider.key} run response 应成功`);
    const runResponse = JSON.parse(runResponses[0].body || "{}");
    assert.strictEqual(runResponse.providerKind, "api-chat", `${provider.key} 后端应分流到 api-chat`);
    assert.strictEqual(runResponse.runtime?.transport, "api-chat", `${provider.key} 不应启动 ACP/OpenClaw runtime`);
    assert.strictEqual(runResponse.runtime?.model, provider.expectedModel, `${provider.key} model 应匹配 registry`);
    assert.strictEqual(runResponse.profile, provider.expectedProfile, `${provider.key} 应使用 isolated API profile`);

    const assistantTexts = await page.locator(".wolai-page-ai-message--assistant").allTextContents();
    const markerAssistantCount = assistantTexts.filter((text) => text.includes(marker)).length;
    assert.strictEqual(markerAssistantCount, 1, `${provider.key} 可见 API 回复应只有一条`);

    const sessionId = String(run.sessionId);
    const bindingRows = sqliteJson(
      `SELECT mnote_session_id, provider, status FROM ai_external_conversation_bindings WHERE user_id=${sqlQuote("mnote-e2e")} AND mnote_session_id=${sqlQuote(sessionId)} ORDER BY updated_at DESC LIMIT 5;`,
      [],
    );
    assert.strictEqual(bindingRows.length, 0, `${provider.key} API ChatOnly 不应写网页 provider conversation binding`);

    await page.reload({ waitUntil: "domcontentloaded", timeout: UI_TIMEOUT_MS });
    await ensurePageAiDrawerOpen(page);
    await waitForAssistantMarker(page, marker);
    const afterReloadScreenshot = await saveScreenshot(page, `${provider.key}-after-reload`);
    const reloadedAssistantTexts = await page.locator(".wolai-page-ai-message--assistant").allTextContents();
    assert.strictEqual(
      reloadedAssistantTexts.filter((text) => text.includes(marker)).length,
      1,
      `${provider.key} 刷新恢复后仍应只有一条助手回复`,
    );

    await page.locator('.wolai-page-ai-header-actions [data-page-ai-action="history"]').click({ timeout: UI_TIMEOUT_MS });
    await page.locator('[data-page-ai-panel="history"]').waitFor({ state: "visible", timeout: UI_TIMEOUT_MS });
    await page.locator(`[data-page-ai-session-row="${sessionId}"]`).waitFor({ state: "visible", timeout: UI_TIMEOUT_MS });
    page.once("dialog", async (dialog) => {
      await dialog.accept();
    });
    await page.locator(`[data-page-ai-session-delete="${sessionId}"]`).click({ timeout: UI_TIMEOUT_MS });
    await page.waitForFunction(
      (id) => !document.querySelector(`[data-page-ai-session-row="${CSS.escape(id)}"]`),
      sessionId,
      { timeout: UI_TIMEOUT_MS },
    );
    const afterDeleteScreenshot = await saveScreenshot(page, `${provider.key}-after-delete`);

    assert(deleteResponses.length >= 1, `${provider.key} 应发出本地 session DELETE 请求`);
    assert.strictEqual(deleteResponses.at(-1).status, 200, `${provider.key} DELETE 应成功`);
    const deleteBody = JSON.parse(deleteResponses.at(-1).body || "{}");
    assert.strictEqual(deleteBody?.result?.remoteDelete?.attempted, false, `${provider.key} 不应调用网页远端删除`);
    assert.strictEqual(
      deleteBody?.result?.remoteDelete?.reason,
      "api_chat_has_no_remote_conversation",
      `${provider.key} remoteDelete reason 应说明 API Chat 无远端会话`,
    );

    const remainingRows = sqliteJson(
      `SELECT session_id, status FROM ai_runtime_runs WHERE user_id=${sqlQuote("mnote-e2e")} AND session_id=${sqlQuote(sessionId)} AND deleted_at IS NULL LIMIT 5;`,
      [],
    );
    assert.strictEqual(remainingRows.length, 0, `${provider.key} 删除后 SQLite active run 不应残留`);

    return {
      provider: provider.key,
      profileId: provider.profileId,
      model: provider.expectedModel,
      sessionId,
      runId: runResponse.runId,
      screenshots: {
        afterMessage: afterMessageScreenshot,
        afterReload: afterReloadScreenshot,
        afterDelete: afterDeleteScreenshot,
      },
    };
  } finally {
    await page.unroute("**/api/hermes/client/runs", runRoute).catch(() => {});
    page.off("response", responseListener);
  }
}

async function main() {
  fs.mkdirSync(OUTPUT_DIR, { recursive: true });
  const suffix = Date.now().toString(36);
  const actorId = "mnote-e2e";
  const root = fs.mkdtempSync(path.join(os.tmpdir(), "mnote-task527-api-chat-"));
  const rootUri = fileUrl(root);
  const workspaceId = `local-ws:${actorId}:task527-api-chat-${suffix}`;
  const relativePath = "ApiChatOnly.md";

  writeWorkspaceManifest(root, actorId, workspaceId);
  fs.writeFileSync(path.join(root, relativePath), ["# API ChatOnly", "", `MNOTE_API_CHAT_WORKSPACE_${suffix}`, ""].join("\n"), "utf8");
  grantWorkspaceAccess({
    actorId,
    workspaceId,
    root,
    rootUri,
    grantId: `grant_task527_api_chat_${suffix}`,
  });

  const browser = await chromium.launch({
    headless: true,
    ...(CHROMIUM_EXECUTABLE_PATH ? { executablePath: CHROMIUM_EXECUTABLE_PATH } : {}),
  });
  const context = await browser.newContext({
    viewport: { width: 1440, height: 960 },
    locale: "zh-CN",
    extraHTTPHeaders: {
      "x-mnote-actor-id": actorId,
      "x-mnote-actor-type": "user",
    },
  });
  const page = await context.newPage();
  let caughtError = null;
  const results = [];

  try {
    await ensureAuthenticated(page, context.request);
    const response = await page.goto(documentUrl(root, relativePath), {
      waitUntil: "domcontentloaded",
      timeout: UI_TIMEOUT_MS,
    });
    assert(response && response.status() === 200, `文档页状态码异常: ${response && response.status()}`);
    await page.locator('[data-testid="mnote-leptos-tiptap-island-editor-root"] .ProseMirror').first().waitFor({
      state: "visible",
      timeout: UI_TIMEOUT_MS,
    });
    await ensurePageAiDrawerOpen(page);
    await saveScreenshot(page, "initial-drawer");

    for (const provider of PROVIDERS) {
      results.push(await runProviderSmoke(page, provider, suffix));
    }
  } catch (error) {
    caughtError = error;
    await saveScreenshot(page, "failure").catch(() => undefined);
  } finally {
    await browser.close().catch(() => {});
  }

  const resultPayload = {
    ok: !caughtError,
    error: caughtError ? String(caughtError && caughtError.stack || caughtError) : "",
    root,
    workspaceId,
    relativePath,
    providers: results,
    outputDir: OUTPUT_DIR,
    resultPath: RESULT_PATH,
  };
  fs.writeFileSync(RESULT_PATH, `${JSON.stringify(resultPayload, null, 2)}\n`, "utf8");
  if (caughtError) {
    console.error(JSON.stringify(resultPayload, null, 2));
    process.exit(1);
  }
  console.log(JSON.stringify(resultPayload, null, 2));
}

main().catch((error) => {
  console.error(error);
  process.exit(1);
});
