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

const assert = require("node:assert/strict");
const fs = require("node:fs");
const http = require("node:http");
const net = require("node:net");
const os = require("node:os");
const path = require("node:path");
const { spawn } = require("node:child_process");
const { chromium } = require("playwright");

const TIMEOUT_MS = Number(process.env.MNOTE_SMOKE_UI_TIMEOUT_MS || 30_000);
const OUTPUT_DIR = path.join(process.cwd(), "tmp", "task541-page-aggregate-local-first-hard-guard-smoke");
const RESULT_PATH = path.join(OUTPUT_DIR, "result.json");
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));

function pickPort() {
  return new Promise((resolve, reject) => {
    const server = net.createServer();
    server.listen(0, "127.0.0.1", () => {
      const address = server.address();
      const port = address && typeof address === "object" ? address.port : 0;
      server.close(() => resolve(port));
    });
    server.on("error", reject);
  });
}

function waitForHttpOk(url, timeoutMs) {
  const deadline = Date.now() + timeoutMs;
  return new Promise((resolve, reject) => {
    const tick = () => {
      const request = http.get(url, (response) => {
        response.resume();
        if (response.statusCode >= 200 && response.statusCode < 500) {
          resolve();
          return;
        }
        retry();
      });
      request.on("error", retry);
      request.setTimeout(1000, () => {
        request.destroy();
        retry();
      });
    };
    const retry = () => {
      if (Date.now() > deadline) {
        reject(new Error(`server_not_ready: ${url}`));
        return;
      }
      setTimeout(tick, 250);
    };
    tick();
  });
}

function actorHeaders(actorId) {
  return {
    "content-type": "application/json",
    "x-mnote-actor-id": actorId,
    "x-mnote-actor-type": "user",
  };
}

async function requestJson(baseUrl, actorId, pathname, options = {}) {
  const response = await fetch(`${baseUrl}${pathname}`, {
    method: options.method || "GET",
    headers: actorHeaders(actorId),
    body: options.body == null ? undefined : JSON.stringify(options.body),
  });
  const payload = await response.json().catch(() => null);
  assert(response.ok, `${options.method || "GET"} ${pathname} failed ${response.status}: ${JSON.stringify(payload)}`);
  return payload;
}

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

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

async function quickLogin(page, baseUrl) {
  await page.goto(`${baseUrl}/auth`, { waitUntil: "domcontentloaded", timeout: TIMEOUT_MS });
  const quickLoginButton = page.getByRole("button", { name: "测试账号快速登录" });
  if (await quickLoginButton.count()) {
    await quickLoginButton.click({ timeout: TIMEOUT_MS });
    await page.waitForURL((url) => url.pathname === "/", { timeout: TIMEOUT_MS });
  }
}

async function main() {
  fs.mkdirSync(OUTPUT_DIR, { recursive: true });
  const port = await pickPort();
  const baseUrl = `http://127.0.0.1:${port}`;
  const dataRoot = fs.mkdtempSync(path.join(os.tmpdir(), "mnote-page-aggregate-hard-guard-"));
  const actorId = `task541-${process.pid}-${Date.now()}`;
  const relativePath = "README.md";
  const managedRoot = path.join(dataRoot, "users", actorId, "workspaces", "my-space");
  const markdownPath = path.join(managedRoot, relativePath);
  const server = spawn("cargo", ["run", "-q", "-p", "mnote-web", "--bin", "mnote-web"], {
    cwd: path.join(__dirname, "..", "rust"),
    env: {
      ...process.env,
      MNOTE_WEB_BIND: `127.0.0.1:${port}`,
      MNOTE_WEB_PUBLIC_BIND: `127.0.0.1:${port}`,
      MNOTE_WEB_ENABLE_LEGACY_NEXT_COMPAT: "0",
      MNOTE_LOCAL_WORKSPACE_BASE_DIR: dataRoot,
    },
    stdio: ["ignore", "pipe", "pipe"],
  });
  let stderr = "";
  server.stderr.on("data", (chunk) => {
    stderr += chunk.toString();
  });

  let browser = null;
  try {
    await waitForHttpOk(`${baseUrl}/health`, 90_000);
    const created = await requestJson(baseUrl, actorId, "/api/local-folder/workspaces/default", {
      method: "POST",
      body: {},
    });
    const rootUri = created.workspace.rootUri;
    fs.mkdirSync(path.dirname(markdownPath), { recursive: true });
    fs.writeFileSync(markdownPath, "# Local Guard\n\nlocal-first browser hard guard\n", "utf8");

    browser = await chromium.launch({
      headless: true,
      ...(CHROMIUM_EXECUTABLE_PATH ? { executablePath: CHROMIUM_EXECUTABLE_PATH } : {}),
    });
    const context = await browser.newContext({
      viewport: { width: 1280, height: 860 },
      extraHTTPHeaders: actorHeaders(actorId),
    });
    const page = await context.newPage();
    await quickLogin(page, baseUrl);
    await page.goto(documentUrl(baseUrl, rootUri, relativePath), {
      waitUntil: "domcontentloaded",
      timeout: TIMEOUT_MS,
    });
    const editorRoot = page.locator('[data-testid="mnote-leptos-tiptap-island-editor-root"]').first();
    await editorRoot.waitFor({ state: "visible", timeout: TIMEOUT_MS });
    await page.waitForFunction(() => {
      const root = document.querySelector('[data-testid="mnote-leptos-tiptap-island-editor-root"]');
      return root?.getAttribute("data-runtime-editor-status") === "ready"
        && root?.getAttribute("data-mnote-page-body-source") === "page_aggregate.block_document";
    }, null, { timeout: TIMEOUT_MS });

    const diagnostic = await page.evaluate(() => {
      const root = document.querySelector('[data-testid="mnote-leptos-tiptap-island-editor-root"]');
      const editor = document.querySelector(".document-pane[data-pane-role=\"primary\"] .editor-surface .ProseMirror");
      return {
        status: root?.getAttribute("data-runtime-editor-status") || "",
        pageBodySource: root?.getAttribute("data-mnote-page-body-source") || "",
        localCompatFallback: root?.getAttribute("data-mnote-page-body-local-compat-fallback") || "",
        hardGuard: root?.getAttribute("data-mnote-page-body-hard-guard") || "",
        projectionSource: root?.getAttribute("data-mnote-projection-source") || "",
        blockProjectionVersion: root?.getAttribute("data-mnote-block-projection-version") || "",
        text: editor?.textContent || "",
      };
    });

    assert.equal(diagnostic.pageBodySource, "page_aggregate.block_document");
    assert.equal(diagnostic.localCompatFallback, "false");
    assert.equal(diagnostic.hardGuard, "local_ok");
    assert.equal(diagnostic.projectionSource, "local_markdown.content");
    assert(diagnostic.blockProjectionVersion, "local-first blockProjectionVersion 应有诊断值");
    assert.match(diagnostic.text, /local-first browser hard guard/);

    fs.writeFileSync(RESULT_PATH, `${JSON.stringify({ baseUrl, rootUri, relativePath, diagnostic }, null, 2)}\n`, "utf8");
    console.log(`task541 page aggregate local-first hard guard smoke passed ${RESULT_PATH}`);
  } finally {
    if (browser) await browser.close().catch(() => {});
    server.kill("SIGINT");
    fs.rmSync(dataRoot, { recursive: true, force: true });
    if (server.exitCode == null) {
      await new Promise((resolve) => server.once("exit", resolve));
    }
    if (server.exitCode && server.exitCode !== 130 && server.exitCode !== null) {
      process.stderr.write(stderr);
    }
  }
}

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