import assert from "node:assert/strict";
import { existsSync, 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, "\\$&"));
}

function parseTsv(text) {
  const [headerLine, ...lines] = text.trim().split(/\r?\n/);
  const headers = headerLine.split("\t");
  return lines.map((line) => {
    const cells = line.split("\t");
    return Object.fromEntries(headers.map((header, index) => [header, cells[index] ?? ""]));
  });
}

const docText = readFileSync(resolve(root, "docs/sim-configs-coverage-handoff.md"), "utf8");
const matrixText = readFileSync(resolve(root, "docs/sim-configs-coverage-matrix.md"), "utf8");
const completionPlanText = readFileSync(resolve(root, "docs/sim-configs-completion-plan.md"), "utf8");
const inventoryTestText = readFileSync(
  resolve(root, "tests/wasm/node/verify_sim_configs_inventory_wasm.mjs"),
  "utf8",
);
const hostSmokeText = readFileSync(resolve(root, "tests/host/verify_host_smokes.sh"), "utf8");
const trackerText = readFileSync(resolve(root, "../PROJECT_COMPLETION_TRACKER.md"), "utf8");

for (const phrase of [
  "Sim-config coverage release handoff",
  "sim_configs_wasm_node_inventory_executed=28",
  "sim_configs_wasm_node_inventory_passed=28",
  "sim_configs_wasm_node_inventory_skipped=131",
  "sim_configs_wasm_node_inventory_unexpected_fail=0",
  "sim_configs_wasm_node_inventory_skip_ASSET_ONLY=65",
  "sim_configs_wasm_node_inventory_skip_L4_PYTHON_REMAP=53",
  "sim_configs_wasm_node_inventory_skip_L4_TOOL_DB=1",
  "sim_configs_wasm_node_inventory_skip_L4_USER_M_PROCESS=1",
  "sim_configs_wasm_node_inventory_skip_NON_MAIN_CLASS=10",
  "sim_configs_wasm_node_inventory_skip_UPSTREAM_DEMO=1",
  "build/wasm/sim-configs-inventory/skip-summary.tsv",
  "build/wasm/sim-configs-inventory/blocked-dependency-summary.tsv",
  "verify_no_standalone_cnc_semantics.sh",
  "verify_sim_configs_coverage_docs.sh",
  "L4-PYTHON-REMAP",
  "L4-TOOL-DB",
  "L4-USER-M-PROCESS",
]) {
  assert.match(docText, literalRegExp(phrase));
}

for (const reason of [
  "ASSET-ONLY",
  "L4-PYTHON-REMAP",
  "L4-TOOL-DB",
  "L4-USER-M-PROCESS",
  "NON_MAIN_CLASS",
  "UPSTREAM-DEMO",
]) {
  assert.match(matrixText, literalRegExp(reason));
  assert.match(completionPlanText, literalRegExp(reason));
  assert.match(docText, literalRegExp(reason));
}

assert.match(inventoryTestText, /sim_configs_wasm_node_inventory_skip_/);
assert.match(hostSmokeText, /verify_sim_configs_inventory_wasm\.sh/);
assert.match(hostSmokeText, /verify_sim_configs_coverage_docs\.sh/);
assert.match(trackerText, /Sim config coverage|sim-config coverage/);

const skipSummaryPath = resolve(root, "build/wasm/sim-configs-inventory/skip-summary.tsv");
if (existsSync(skipSummaryPath)) {
  const rows = parseTsv(readFileSync(skipSummaryPath, "utf8"));
  assert.deepEqual(
    Object.fromEntries(rows.map((row) => [row.skip_or_block_reason, Number(row.count)])),
    {
      "ASSET-ONLY": 65,
      "L4-PYTHON-REMAP": 53,
      "L4-TOOL-DB": 1,
      "L4-USER-M-PROCESS": 1,
      NON_MAIN_CLASS: 10,
      "UPSTREAM-DEMO": 1,
    },
  );
}

console.log("sim_configs_coverage_docs_node_smoke=ok");
