import assert from "node:assert/strict";
import { readFileSync } from "node:fs";
import { dirname, resolve } from "node:path";
import { fileURLToPath } from "node:url";

const __dirname = dirname(fileURLToPath(import.meta.url));
const root = resolve(__dirname, "../../..");

function literalRegExp(text) {
  return new RegExp(text.replace(/[.*+?^${}()|[\]\\]/g, "\\$&"));
}

const handoffText = readFileSync(resolve(root, "docs/project-release-handoff.md"), "utf8");
const readmeText = readFileSync(resolve(root, "README.md"), "utf8");
const panelEntryText = readFileSync(resolve(root, "docs/panel-entry.md"), "utf8");
const sdkReadmeText = readFileSync(resolve(root, "runtime/sdk/README.md"), "utf8");
const sourceReuseText = readFileSync(resolve(root, "docs/source-reuse-map.md"), "utf8");
const driftText = readFileSync(resolve(root, "docs/drift-report.md"), "utf8");
const hostSmokeText = readFileSync(resolve(root, "tests/host/verify_host_smokes.sh"), "utf8");
const projectReleaseGateText = readFileSync(resolve(root, "tests/host/verify_project_release_gate.sh"), "utf8");
const trackerText = readFileSync(resolve(root, "../PROJECT_COMPLETION_TRACKER.md"), "utf8");

for (const phrase of [
  "Project release handoff",
  "docs/panel-entry.md",
  "runtime/sdk/README.md",
  "docs/opfs-session-persistence.md",
  "docs/sim-configs-coverage-handoff.md",
  "docs/host-runtime-boundary-handoff.md",
  "docs/source-reuse-map.md",
  "docs/drift-report.md",
  "PROJECT_COMPLETION_TRACKER.md",
  "runtime/sdk/src/index.js",
  "createIniPanelShellSessionReadinessWorkflowReport()",
  "workflow overview embedding mount DOM",
  "verify_no_standalone_cnc_semantics.sh",
  "verify_host_smokes.sh",
  "verify_project_release_gate.sh",
  "host_wasm_opfs_browser_smokes=ok",
  "project_release_gate=ok",
  "sim_configs_wasm_node_inventory_unexpected_fail=0",
  "browser_ini_shell_integration_workflow_smoke=ok",
  "project_release_handoff_docs_node_smoke=ok",
  "L4-USER-M-PROCESS",
  "L4-TOOL-DB",
  "L4-PYTHON-REMAP",
]) {
  assert.match(handoffText, literalRegExp(phrase));
}

for (const phrase of [
  "docs/project-release-handoff.md",
  "project_release_handoff_docs_node_smoke=ok",
]) {
  assert.match(readmeText, literalRegExp(phrase));
  assert.match(hostSmokeText, literalRegExp("verify_project_release_handoff_docs.sh"));
}

for (const phrase of [
  "git diff --check",
  "verify_vendor_sync.sh",
  "verify_no_standalone_cnc_semantics.sh",
  "verify_interp_wasm.sh",
  "verify_sim_configs_inventory_wasm.sh",
  "verify_ini_panel_browser.sh",
  "verify_ui_node_smokes.sh",
  "verify_host_smokes.sh",
  "project_release_gate=ok",
]) {
  assert.match(projectReleaseGateText, literalRegExp(phrase));
}

for (const phrase of [
  "docs/project-release-handoff.md",
  "runtime/sdk/src/index.js",
  "workflow overview embedding",
]) {
  assert.match(panelEntryText, literalRegExp(phrase));
}

for (const phrase of [
  "OPFS/session persistence exports",
  "INI panel shell handoff exports",
  "createIniPanelShellSessionReadinessWorkflowReport()",
]) {
  assert.match(sdkReadmeText, literalRegExp(phrase));
}

assert.match(sourceReuseText, literalRegExp("docs/project-release-handoff.md"));
assert.match(driftText, literalRegExp("docs/project-release-handoff.md"));
assert.match(trackerText, /Documentation\/release gate/);
assert.match(trackerText, /Project release gate wrapper/);
assert.match(trackerText, /Final Acceptance Checklist/);

console.log("project_release_handoff_docs_node_smoke=ok");
