#!/usr/bin/env node
import { createHash } from "node:crypto";
import { spawnSync } from "node:child_process";
import { cp, 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 here = dirname(fileURLToPath(import.meta.url));
const platformRoot = resolve(here, "../..");
const engineRoot = resolve(
  process.env.NODEDC_ENGINE_SOURCE_ROOT || resolve(platformRoot, "../NODEDC_ENGINE_INFRA"),
);
const canonicalArtifactRoot = resolve(here, "../deploy-artifacts");
const artifactRoot = resolve(process.env.NODEDC_DEPLOY_ARTIFACT_DIR || canonicalArtifactRoot);
const [transitionId = "20260718-003", ...extra] = process.argv.slice(2);
if (extra.length || !/^\d{8}-[0-9]{3}$/.test(transitionId)) {
  throw new Error("usage: build-engine-mcp-control-plane-artifact.mjs [YYYYMMDD-NNN]");
}

const id = `engine-mcp-control-plane-${transitionId}`;
const target = join(artifactRoot, `nodedc-${id}.tgz`);
const nodeIntelligenceArtifact = join(
  canonicalArtifactRoot,
  "nodedc-engine-node-intelligence-20260717-001.tgz",
);
const nodeIntelligenceArtifactSha256 = "d126afaa0c714fef26362aad4749e3f4647f551d6eb975f0133cdf9ff2e6fc4f";
const descriptorRel = "nodedc-source/services/node-intelligence/activation.json";
const gatewayRel = "nodedc-source/server/routes/engineAgentGateway.js";
const upstreamProjectionRel = "nodedc-source/server/nodeIntelligence/upstreamProjection.js";
const files = [
  "nodedc-source/server/assets/engine-agent-npm/bin/nodedc-engine-codex-agent.mjs",
  "nodedc-source/server/assets/engine-agent-npm/package.json",
  "nodedc-source/server/assets/nodedc-engine-codex-agent-0.1.4.tgz",
  "nodedc-source/server/dataProductPublishGrant/providerCatalog.js",
  "nodedc-source/server/dataProductPublishGrant/signedDataPlaneClient.js",
  "nodedc-source/server/dataProductReadGrant/acceptance.js",
  "nodedc-source/server/dataProductReadGrant/service.js",
  "nodedc-source/server/dataProductReadGrant/store.js",
  "nodedc-source/server/engineAgents/store.js",
  upstreamProjectionRel,
  gatewayRel,
  "nodedc-source/server/routes/n8n.js",
  "nodedc-source/server/routes/ndcAgentMcp.js",
  "nodedc-source/services/backend/data-product-read-grant/docker-compose.immutable-runtime.yml",
  "nodedc-source/server/dataProductPublishGrant/service.js",
  "nodedc-source/server/dataProductPublishGrant/store.js",
  descriptorRel,
];
const expectedSha256 = new Map([
  [files[0], "521098de69fe288a56bd4158c849c1055ba24be08e19f7a4111618d0e8138445"],
  [files[1], "cbe113e9b10bb84b9ccbffa3e261908e58a8430c11abe2cf4fd5304741b04597"],
  [files[2], "e74c0136d346f904b42589d75cb11a952b4d2f21153f1b057fba28e9acf4f95f"],
  [files[3], "901b8fad80018ce177b34ced804b39cb140a47e831414057f484296b373c651d"],
  [files[4], "c2a13d5eb49937fe70ec6451d0465cac54c19c7fae44448db099079473ec02fc"],
  [files[5], "202dbe575b2f2e1c584aa7e6e99e38fa84f12496feb454e3dbd3f98e2d0396dd"],
  [files[6], "65b8cdd6b603333bac1feb303e9791feb1e9da39dc9b5bfc3df3961273b0052e"],
  [files[7], "c3fcdc59a794f57d92e3d2ac713ee28341347cebd25364c4060beaa3dc2151de"],
  [files[8], "debd351fe0b8c72b33b8c79909b8f339cbaf061b970576f3ae72df52ebaa211f"],
  [files[9], "761a874b102a938bc6018159ddacdaac71ad6ae08e9f0f8d7f3b58a0165a5131"],
  [files[10], "96c726dab5cf1341f74e6e1095d518058ca320e0dd5738e25bdbe75db1f4fc15"],
  [files[11], "f293a7794405badbabd2bf7ef088f96fe1167d9e249f05cbfc8af280a0d3e8f8"],
  [files[12], "534e2c85e1faecc72a00da7ad32d0584ee09b6bd89eeec2221c3b23d80e1e962"],
  [files[13], "952df0cb2ac477e644f5f3a9d64b4872ce1da33356eeab0bec17dcb2931cf653"],
  [files[14], "ded3832c9677345a988448ae8e69cef254fd9bf39d2de20cffa295c1feedcd7c"],
  [files[15], "a92303b2732e21f68c1ac732fa26e4983cbadf3515741cee983b916a283d754c"],
]);

await assertFresh(target);
assertSha(await readFile(nodeIntelligenceArtifact), nodeIntelligenceArtifactSha256, "node intelligence artifact");
const stage = await mkdtemp(join(tmpdir(), "nodedc-engine-mcp-control-plane-"));
const payload = join(stage, "payload");
try {
  await mkdir(payload, { recursive: true });
  for (const rel of files.slice(0, -1)) {
    const source = join(engineRoot, rel);
    const stat = await lstat(source);
    if (!stat.isFile() || stat.isSymbolicLink()) throw new Error(`source_boundary_invalid:${rel}`);
    assertSha(await readFile(source), expectedSha256.get(rel), rel);
    await mkdir(dirname(join(payload, rel)), { recursive: true });
    await cp(source, join(payload, rel), { force: false });
  }

  const descriptorText = extractMember(
    nodeIntelligenceArtifact,
    `payload/${descriptorRel}`,
  );
  const descriptor = JSON.parse(descriptorText);
  if (
    descriptor?.action !== "activate"
    || descriptor?.releaseId !== "2.33.2-974a9fb3492f"
    || descriptor?.source?.gatewaySha256 !== "9642c76fd5765a8a20629c8d09e16579a1c3b2cfb7bdbab38cf55af469d8908f"
  ) throw new Error("node_intelligence_predecessor_descriptor_mismatch");
  descriptor.source.gatewaySha256 = expectedSha256.get(gatewayRel);
  descriptor.source.upstreamProjectionSha256 = expectedSha256.get(upstreamProjectionRel);
  await mkdir(dirname(join(payload, descriptorRel)), { recursive: true });
  await writeFile(join(payload, descriptorRel), `${JSON.stringify(descriptor, null, 2)}\n`, "utf8");

  await writeFile(join(stage, "manifest.env"), `id=${id}\ncomponent=engine\ntype=app-overlay\n`, "utf8");
  await writeFile(join(stage, "files.txt"), `${files.join("\n")}\n`, "utf8");
  await mkdir(artifactRoot, { recursive: true });
  run("python3", ["-c", canonicalTarScript(), target, stage]);
  const artifactSha256 = sha(await readFile(target));
  console.log(JSON.stringify({
    ok: true,
    id,
    artifact: target,
    artifactSha256,
    services: ["nodedc-backend"],
    predecessorGatewaySha256: "9642c76fd5765a8a20629c8d09e16579a1c3b2cfb7bdbab38cf55af469d8908f",
    targetGatewaySha256: expectedSha256.get(gatewayRel),
    mcpVersion: "0.5.0",
    installerVersion: "0.1.4",
    files,
  }, null, 2));
} finally {
  await rm(stage, { recursive: true, force: true });
}

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

function extractMember(archive, member) {
  const script = [
    "import pathlib,sys,tarfile",
    "p=pathlib.Path(sys.argv[1]); name=sys.argv[2]",
    "with tarfile.open(p,'r:gz') as t:",
    " m=t.getmember(name)",
    " if not m.isfile(): raise SystemExit('member-not-file')",
    " f=t.extractfile(m)",
    " if f is None: raise SystemExit('member-unreadable')",
    " sys.stdout.buffer.write(f.read())",
  ].join("\n");
  return run("python3", ["-c", script, archive, member]).stdout;
}

function canonicalTarScript() {
  return [
    "import gzip,io,pathlib,sys,tarfile",
    "root=pathlib.Path(sys.argv[2])",
    "with open(sys.argv[1],'wb') 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 assertSha(bytes, expected, label) {
  const actual = sha(bytes);
  if (!expected || actual !== expected) throw new Error(`${label.replaceAll(" ", "_")}_sha256_mismatch:${actual}`);
}

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

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