#!/usr/bin/env node

import { createHash } from "node:crypto";
import { spawnSync } from "node:child_process";
import {
  copyFile,
  lstat,
  mkdir,
  mkdtemp,
  readFile,
  rm,
  writeFile,
} from "node:fs/promises";
import { tmpdir } from "node:os";
import { dirname, join, resolve } from "node:path";
import { fileURLToPath } from "node:url";

const scriptDir = dirname(fileURLToPath(import.meta.url));
const workspaceRoot = resolve(scriptDir, "../../..");
const engineRoot = resolve(
  process.env.NODEDC_ENGINE_SOURCE_ROOT
    || join(workspaceRoot, "NODEDC_ENGINE_INFRA"),
);
const artifactDir = resolve(
  process.env.NODEDC_DEPLOY_ARTIFACT_DIR
    || resolve(scriptDir, "../deploy-artifacts"),
);
const [patchId = "engine-l1-agent-init-concurrency-20260724-042", ...extra] =
  process.argv.slice(2);

if (
  extra.length
  || !/^engine-l1-agent-init-concurrency-\d{8}-\d{3}$/.test(patchId)
) {
  throw new Error(
    "usage: build-engine-l1-agent-init-concurrency-artifact.mjs "
      + "[engine-l1-agent-init-concurrency-YYYYMMDD-NNN]",
  );
}

const sourceCommit = "c4077fe0d8d824fa58616eeb986dc78ca43070da";
const predecessorSha256 = Object.freeze({
  "nodedc-source/src/App.tsx":
    "b3e61a89330b774f309660208d05143d299ab582f18765c1c14e2122a62c4d5e",
  "nodedc-source/src/driveinspector/nodes/N8n.definition.ts":
    "a4890cca12b269643ec3255c2fc6b7e2be96aa042ffc6087772e63cbf8323c3a",
  "nodedc-source/src/nodes/N8nNode.tsx":
    "a125f7cde66085008dafe5a340a1143094c59769573815851ce6c8adfe4c0220",
  "nodedc-source/src/store.ts":
    "2128cd582a947582bb7736792c3a71551712262ef04882c189220ec79f16fb8e",
  "nodedc-source/dist/index.html":
    "90d72f8790dc8cf951066b1210447c84d640373117bae23f3a4cb3227fb96d6d",
  "nodedc-source/dist/assets/index-CqvJfRRS.js":
    "06e6c23b03ea2ba8a4890e1f1714fd08ebaf18d735834200af6ab430af360ca8",
});
const targetSha256 = Object.freeze({
  "nodedc-source/src/App.tsx":
    "fc580dc3a1a97393af81dd75c54ae8b37db1f6fda8364042198493493b15f2c2",
  "nodedc-source/src/driveinspector/nodes/N8n.definition.ts":
    "945839541547b2fd02b5424954e00483d6c76f82a333d6a7a652ccd1b74d04cf",
  "nodedc-source/src/nodes/N8nNode.tsx":
    "1130e355d86e06a18df775b2d3517bfdbf98fdc494ca707d23c78648fbfd3cb4",
  "nodedc-source/src/store.ts":
    "46884488254c16cd3af9b25556dd0db4f5a3a20047abdb496db831f204e40673",
  "nodedc-source/src/utils/workflowWriteQueue.js":
    "41f73ccb5e1dafbdae501b7c0b9fa5c47fdbd52564061b9c4ca6ca1c75ceccdf",
  "nodedc-source/dist/index.html":
    "1554e2418aead13b56328c5721dd677400d84ee087559a88fe60b295b7ff5412",
  "nodedc-source/dist/assets/index-r1_2ECzJ.js":
    "0d25dd843da7078b8dbad5cb4b78724bf1ca8aa067e9d0add1e09ce8f421b822",
});
const entries = Object.freeze(Object.keys(targetSha256));
const artifact = join(artifactDir, `nodedc-${patchId}.tgz`);
const checksum = `${artifact}.sha256`;

assertSourceCommit();
await assertPredecessorSources();
await assertExactTargets();
await assertRuntimeContract();
await assertFresh(artifact);

const stage = await mkdtemp(join(tmpdir(), "nodedc-engine-l1-agent-init-"));
try {
  for (const relativePath of entries) {
    const destination = join(stage, "payload", relativePath);
    await mkdir(dirname(destination), { recursive: true });
    await copyFile(join(engineRoot, relativePath), destination);
  }
  await writeFile(
    join(stage, "manifest.env"),
    `id=${patchId}\ncomponent=engine\ntype=app-overlay\n`,
    { encoding: "utf8", flag: "wx", mode: 0o644 },
  );
  await writeFile(join(stage, "files.txt"), `${entries.join("\n")}\n`, {
    encoding: "utf8",
    flag: "wx",
    mode: 0o644,
  });
  await mkdir(artifactDir, { recursive: true });
  run("python3", ["-c", canonicalTarScript(), artifact, stage]);
  const sha256 = digest(await readFile(artifact));
  await writeFile(checksum, `${sha256}  ${artifact.split("/").at(-1)}\n`, {
    encoding: "utf8",
    flag: "wx",
    mode: 0o644,
  });
  console.log(JSON.stringify({
    ok: true,
    patchId,
    artifact,
    checksum,
    sha256,
    sourceCommit,
    predecessorSha256,
    targetSha256,
    entries,
    services: ["nodedc-backend", "app"],
    healthchecks: [
      "http://127.0.0.1:8080/",
      "http://127.0.0.1:3001/health",
    ],
    concurrencyContract: "per-workflow-fifo-latest-revision-v1",
    n8nCoreChanged: false,
    untouched: [
      "n8n runtime",
      "L1 workflow data",
      "L2 workflow data",
      "credentials",
      "databases",
      "MCP catalogs",
    ],
  }, null, 2));
} finally {
  await rm(stage, { recursive: true, force: true });
}

function assertSourceCommit() {
  const actual = run("git", ["-C", engineRoot, "rev-parse", "HEAD"]).stdout.trim();
  if (actual !== sourceCommit) {
    throw new Error(
      `engine_l1_agent_init_source_commit_mismatch:`
        + `expected=${sourceCommit}:actual=${actual}`,
    );
  }
}

async function assertPredecessorSources() {
  for (const [relativePath, expected] of Object.entries(predecessorSha256)) {
    const result = run("git", [
      "-C",
      engineRoot,
      "show",
      `${sourceCommit}:${relativePath}`,
    ]);
    const actual = digest(result.stdoutBuffer);
    if (actual !== expected) {
      throw new Error(
        `engine_l1_agent_init_predecessor_mismatch:${relativePath}:`
          + `expected=${expected}:actual=${actual}`,
      );
    }
  }
  const newFile = "nodedc-source/src/utils/workflowWriteQueue.js";
  const probe = spawnSync(
    "git",
    ["-C", engineRoot, "cat-file", "-e", `${sourceCommit}:${newFile}`],
    { encoding: "utf8", stdio: ["ignore", "pipe", "pipe"] },
  );
  if (probe.status === 0) {
    throw new Error("engine_l1_agent_init_queue_not_new");
  }
}

async function assertExactTargets() {
  for (const [relativePath, expected] of Object.entries(targetSha256)) {
    const source = join(engineRoot, relativePath);
    const info = await lstat(source);
    if (!info.isFile() || info.isSymbolicLink()) {
      throw new Error(`engine_l1_agent_init_source_unsafe:${relativePath}`);
    }
    const actual = digest(await readFile(source));
    if (actual !== expected) {
      throw new Error(
        `engine_l1_agent_init_target_mismatch:${relativePath}:`
          + `expected=${expected}:actual=${actual}`,
      );
    }
  }
}

async function assertRuntimeContract() {
  const app = await readFile(join(engineRoot, "nodedc-source/src/App.tsx"), "utf8");
  const definition = await readFile(
    join(engineRoot, "nodedc-source/src/driveinspector/nodes/N8n.definition.ts"),
    "utf8",
  );
  const store = await readFile(join(engineRoot, "nodedc-source/src/store.ts"), "utf8");
  const node = await readFile(
    join(engineRoot, "nodedc-source/src/nodes/N8nNode.tsx"),
    "utf8",
  );
  const queue = await readFile(
    join(engineRoot, "nodedc-source/src/utils/workflowWriteQueue.js"),
    "utf8",
  );
  const index = await readFile(
    join(engineRoot, "nodedc-source/dist/index.html"),
    "utf8",
  );

  if (
    !app.includes("createWorkflowWriteQueue")
    || !app.includes("putExistingWorkflow")
    || !app.includes("expectedRevision: currentWorkflowRevisionRef.current")
    || !queue.includes("previous.catch(() => undefined).then(() => operation())")
    || !definition.includes("nodeName: 'NDC'")
    || !definition.includes("default: 'NDC'")
    || !definition.includes("data?.title || data?.nodeName || 'NDC'")
    || !store.includes("nodeName: 'NDC'")
    || !node.includes("data?.nodeName ?? 'NDC'")
    || !index.includes("/assets/index-r1_2ECzJ.js")
    || /gelios|robot2b/i.test(queue)
  ) {
    throw new Error("engine_l1_agent_init_runtime_contract_invalid");
  }
}

async function assertFresh(path) {
  try {
    await lstat(path);
  } catch (error) {
    if (error?.code === "ENOENT") return;
    throw error;
  }
  throw new Error("artifact_already_exists");
}

function canonicalTarScript() {
  return [
    "import gzip,io,pathlib,sys,tarfile",
    "root=pathlib.Path(sys.argv[2])",
    "with open(sys.argv[1],'xb') as out:",
    " with gzip.GzipFile(filename='',mode='wb',fileobj=out,compresslevel=9,mtime=0) as gz:",
    "  with tarfile.open(fileobj=gz,mode='w',format=tarfile.PAX_FORMAT) as tar:",
    "   for top in ('manifest.env','files.txt','payload'):",
    "    p=root/top; paths=[p]+(sorted(p.rglob('*')) if p.is_dir() else [])",
    "    for x in paths:",
    "     info=tar.gettarinfo(str(x),arcname=x.relative_to(root).as_posix())",
    "     info.uid=info.gid=0; info.uname=info.gname='root'; info.mtime=0; info.mode=0o755 if info.isdir() else 0o644",
    "     with (open(x,'rb') if info.isfile() else io.BytesIO()) as src: tar.addfile(info,src if info.isfile() else None)",
  ].join("\n");
}

function digest(value) {
  return createHash("sha256").update(value).digest("hex");
}

function run(command, args) {
  const result = spawnSync(command, args, {
    encoding: null,
    stdio: ["ignore", "pipe", "pipe"],
    maxBuffer: 128 * 1024 * 1024,
  });
  if (result.status !== 0) {
    throw new Error(
      `${command}_failed:${Buffer.from(result.stderr || "").toString("utf8")}`
        + `${Buffer.from(result.stdout || "").toString("utf8")}`,
    );
  }
  return {
    stdoutBuffer: Buffer.from(result.stdout || ""),
    stderrBuffer: Buffer.from(result.stderr || ""),
    stdout: Buffer.from(result.stdout || "").toString("utf8"),
    stderr: Buffer.from(result.stderr || "").toString("utf8"),
  };
}
