#!/usr/bin/env node

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

const BASE_URL = process.env.MNOTE_WEB_SMOKE_BASE_URL || "http://127.0.0.1:3000";
const UI_TIMEOUT_MS = Number(process.env.UI_TIMEOUT_MS || 10_000);

function fileUrl(filePath) {
  return `file://${filePath.split(path.sep).map((part, index) => (
    index === 0 ? "" : encodeURIComponent(part)
  )).join("/")}`;
}

async function main() {
  const root = fs.mkdtempSync(path.join(os.tmpdir(), "mnote-main-local-folder-"));
  const otherRoot = fs.mkdtempSync(path.join(os.tmpdir(), "mnote-main 本地 #other-"));
  fs.mkdirSync(path.join(root, "docs"), { recursive: true });
  fs.writeFileSync(path.join(root, "README.md"), "# Root Page\n", "utf8");
  fs.writeFileSync(path.join(root, "docs", "child.md"), "# Child Page\n", "utf8");
  fs.writeFileSync(path.join(root, "plain.txt"), "plain asset\n", "utf8");
  fs.writeFileSync(path.join(otherRoot, "OTHER.md"), "# Other Root\n", "utf8");

  const browser = await chromium.launch({ headless: true });
  const context = await browser.newContext({
    extraHTTPHeaders: {
      "x-mnote-actor-id": "user_real",
      "x-mnote-actor-type": "user",
    },
  });
  const page = await context.newPage();
  const dialogs = [];
  page.on("dialog", async (dialog) => {
    dialogs.push(dialog.type());
    await dialog.dismiss().catch(() => {});
  });

  try {
    await page.goto(BASE_URL, { waitUntil: "domcontentloaded", timeout: UI_TIMEOUT_MS });
    await page.locator('[data-mnote-action="open-local-folder"]').click({ timeout: UI_TIMEOUT_MS });
    await page.locator('[data-testid="mnote-local-folder-dialog"]').waitFor({
      state: "visible",
      timeout: UI_TIMEOUT_MS,
    });
    assert.deepStrictEqual(dialogs, [], "打开本地文件夹不能再触发 prompt/alert/confirm 原生弹窗");

    await page.locator('[data-testid="mnote-local-folder-path-input"]').fill(root);
    await page.locator('[data-testid="mnote-local-folder-open-confirm"]').click();
    await page.waitForURL((url) => {
      return url.origin === new URL(BASE_URL).origin &&
        url.pathname === "/" &&
        url.searchParams.get("sourceKind") === "local_folder" &&
        url.searchParams.get("rootUri") === fileUrl(root) &&
        url.searchParams.get("treeView") === "filetree";
    }, { timeout: UI_TIMEOUT_MS });
    const recentLocalRoots = await page.evaluate(() => {
      const raw = window.localStorage.getItem("mnote.localFolder.recentRoots") || "[]";
      return JSON.parse(raw);
    });
    assert.equal(recentLocalRoots[0], fileUrl(root), "打开本地文件夹后应记录最近 rootUri");

    const bodyText = await page.locator("body").innerText({ timeout: UI_TIMEOUT_MS });
    assert(!bodyText.includes("legacy_next_compat_disabled"), "本地文件夹主入口不能落入 legacy Next fallback");
    const readmeRow = page.locator('.tree-row[data-row-id="local:markdown:README.md"]');
    const docsRow = page.locator('.tree-row[data-row-id="local:folder:docs"]');
    await readmeRow.waitFor({
      state: "visible",
      timeout: UI_TIMEOUT_MS,
    });
    await docsRow.waitFor({
      state: "visible",
      timeout: UI_TIMEOUT_MS,
    });

    await readmeRow.locator(":scope > .tree-link").click();
    await readmeRow.waitFor({
      state: "visible",
      timeout: UI_TIMEOUT_MS,
    });
    await docsRow.locator(":scope > .tree-link").click({ modifiers: ["Shift"] });
    const selectedRowIds = await page.locator('#sidebar-file-tree-root .tree-row[data-selected="true"]')
      .evaluateAll((rows) => rows.map((row) => row.getAttribute("data-row-id")));
    assert(
      selectedRowIds.includes("local:markdown:README.md") &&
        selectedRowIds.includes("local:folder:docs") &&
        selectedRowIds.length >= 2,
      `Shift+Click 应形成文件树范围多选，实际选中: ${selectedRowIds.join(", ")}`,
    );

    await page.locator('[data-testid="mnote-workspace-source-trigger"]').click({ timeout: UI_TIMEOUT_MS });
    await page.locator('[data-testid="mnote-workspace-source-menu"]').waitFor({ state: "visible", timeout: UI_TIMEOUT_MS });
    await page.locator(`[data-testid="mnote-recent-local-root"][data-root-uri="${fileUrl(root)}"]`).waitFor({
      state: "visible",
      timeout: UI_TIMEOUT_MS,
    });
    await page.locator('[data-testid="mnote-open-other-local-folder"]').click({ timeout: UI_TIMEOUT_MS });
    await page.locator('[data-testid="mnote-local-folder-dialog"]').waitFor({ state: "visible", timeout: UI_TIMEOUT_MS });
    await page.locator('[data-testid="mnote-local-folder-path-input"]').fill(otherRoot);
    await page.locator('[data-testid="mnote-local-folder-open-confirm"]').click({ timeout: UI_TIMEOUT_MS });
    await page.waitForURL((url) => {
      return url.pathname === "/" &&
        url.searchParams.get("sourceKind") === "local_folder" &&
        url.searchParams.get("rootUri") === fileUrl(otherRoot) &&
        url.searchParams.get("treeView") === "filetree";
    }, { timeout: UI_TIMEOUT_MS });
    const updatedRecentLocalRoots = await page.evaluate(() => {
      const raw = window.localStorage.getItem("mnote.localFolder.recentRoots") || "[]";
      return JSON.parse(raw);
    });
    assert.equal(updatedRecentLocalRoots[0], fileUrl(otherRoot), "切到其他本地文件夹后应把它放到最近目录首位");
    assert.equal(updatedRecentLocalRoots[1], fileUrl(root), "之前打开过的本地目录应保留在最近目录列表中");
    await page.locator('.tree-row[data-row-id="local:markdown:OTHER.md"]').waitFor({
      state: "visible",
      timeout: UI_TIMEOUT_MS,
    });

    await page.locator('[data-testid="mnote-workspace-source-trigger"]').click({ timeout: UI_TIMEOUT_MS });
    await page.locator('[data-testid="mnote-workspace-source-menu"]').waitFor({ state: "visible", timeout: UI_TIMEOUT_MS });
    await page.locator('[data-testid="mnote-open-other-local-folder"]').click({ timeout: UI_TIMEOUT_MS });
    await page.locator('[data-testid="mnote-local-folder-dialog"]').waitFor({ state: "visible", timeout: UI_TIMEOUT_MS });
    const recentPathInputValue = await page.locator('[data-testid="mnote-local-folder-path-input"]').inputValue({
      timeout: UI_TIMEOUT_MS,
    });
    assert.equal(recentPathInputValue, otherRoot, "最近目录回填输入框时应解码 file:// rootUri，避免二次编码");
    await page.getByRole("button", { name: "取消" }).click({ timeout: UI_TIMEOUT_MS });

    await page.locator('[data-testid="mnote-workspace-source-trigger"]').click({ timeout: UI_TIMEOUT_MS });
    await page.locator('[data-testid="mnote-workspace-source-menu"]').waitFor({ state: "visible", timeout: UI_TIMEOUT_MS });
    await page.locator('[data-testid="mnote-switch-cloud-workspace"]').click({ timeout: UI_TIMEOUT_MS });
    await page.waitForURL((url) => {
      return url.pathname === "/" &&
        url.searchParams.get("sourceKind") === "convex_workspace" &&
        !url.searchParams.has("rootUri");
    }, { timeout: UI_TIMEOUT_MS });

    console.log("task164 desktop hot local folder main entry smoke passed");
  } finally {
    await browser.close();
    fs.rmSync(root, { recursive: true, force: true });
    fs.rmSync(otherRoot, { recursive: true, force: true });
  }
}

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