#!/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 here = dirname(fileURLToPath(import.meta.url));
const workspaceRoot = resolve(here, "../../..");
const engineRoot = resolve(
  process.env.NODEDC_ENGINE_SOURCE_ROOT || join(workspaceRoot, "NODEDC_ENGINE_INFRA"),
);
const artifactRoot = resolve(
  process.env.NODEDC_DEPLOY_ARTIFACT_DIR || resolve(here, "../deploy-artifacts"),
);
const baselineArtifact = resolve(
  here,
  "../deploy-artifacts/nodedc-engine-mcp-telemetry-catalog-20260723-035.tgz",
);
const baselineArtifactSha256 =
  "4ce1563612499a19d653a174a4d73632e0aaac7dacd2eca4e7c01c87586b1155";
const nodeIntelligenceDescriptorPath =
  "nodedc-source/services/node-intelligence/activation.json";
const predecessorNodeIntelligenceDescriptorSha256 =
  "25ed3efd858aaf82c242dba501f0acc0c6c3dc91e8845707a4d3325750eab59f";
const targetNodeIntelligenceDescriptorSha256 =
  "03b2ba120c3929e9cf99940ddc927082de376ceff762895a84103144202aef42";
const predecessorGatewaySha256 =
  "69bfc91e913a3fad04e13aca86efb9d62f73c0c7d1f8f7200907494b29fa9e8d";
const targetGatewaySha256 =
  "9020cf49a3558c0497fed4ecfd81367cbc11b5b249881804c29fe437900c71f8";
const [patchId = "", ...extra] = process.argv.slice(2);
if (
  extra.length
  || !/^engine-mcp-execution-plan-materialization-\d{8}-\d{3}$/.test(patchId)
) {
  throw new Error(
    "usage: build-engine-mcp-execution-plan-materialization-artifact.mjs "
    + "<engine-mcp-execution-plan-materialization-YYYYMMDD-NNN>",
  );
}

const descriptorPath =
  "nodedc-source/server/deployTransitions/executionPlanMaterializationV1.json";
const targetSha256 = Object.freeze({
  "nodedc-source/server/routes/n8n.js":
    "391fc81228fdacd6efc6c0868991a3485f71869708e49ac15819db6ccce1bfce",
  "nodedc-source/server/routes/engineAgentGateway.js":
    targetGatewaySha256,
  "nodedc-source/server/l2ExecutionPlan/catalog.js":
    "c35b4c7ad9319aabbf1366a11ff52a6e99d961893bafdfcb4e84fc5f24fc04be",
  "nodedc-source/server/l2ExecutionPlan/compiler.js":
    "beb3f664073f9a372432643936a04d0cb0028cd695f759c68bd053e9cd892fe4",
  "nodedc-source/server/l2ExecutionPlan/materializer.js":
    "ee3bcfd06b3a5fa46800df974a2dedfdd55eeaf662329f837486c011a9bd713e",
  "nodedc-source/server/assets/execution-plans/v1/catalog.json":
    "a153e040e0a592bad4375b98d9a1d83d148923aa0f0fd47d225b92eda281c4e9",
  [descriptorPath]:
    "52c0152cff49c251be5581a9209d2e63ba710c16e815bdd6ece24f7c9dd7e480",
  [nodeIntelligenceDescriptorPath]:
    targetNodeIntelligenceDescriptorSha256,
});
const entries = Object.freeze(Object.keys(targetSha256));
const artifact = join(artifactRoot, `nodedc-${patchId}.tgz`);

await assertFresh(artifact);
await assertExactSources();
const targetNodeIntelligenceDescriptor =
  await buildTargetNodeIntelligenceDescriptor();
const stage = await mkdtemp(
  join(tmpdir(), "nodedc-engine-mcp-execution-plan-materialization-"),
);
try {
  const payload = join(stage, "payload");
  for (const relativePath of entries) {
    const destination = join(payload, relativePath);
    await mkdir(dirname(destination), { recursive: true });
    if (relativePath === nodeIntelligenceDescriptorPath) {
      await writeFile(destination, targetNodeIntelligenceDescriptor, {
        encoding: "utf8",
        flag: "wx",
        mode: 0o644,
      });
      continue;
    }
    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(artifactRoot, { recursive: true });
  run("python3", ["-c", canonicalTarScript(), artifact, stage]);
  console.log(JSON.stringify({
    ok: true,
    patchId,
    artifact,
    sha256: digest(await readFile(artifact)),
    entries,
    targetSha256,
    services: ["nodedc-backend"],
    transition: "telemetry-catalog-v1-to-execution-plan-materialization-v1",
    mcpVersion: "0.9.0",
    mcpSurface: "external-codex",
    mcpTools: [
      "engine_plan_l2_execution_plan_materialization",
      "engine_apply_l2_execution_plan_materialization",
    ],
    providerLogicAuthority: "trusted-provider-package",
    unmanagedGraphPolicy: "explicit-adoption-required",
    nodeIntelligenceRelease: "2.33.2-974a9fb3492f",
    predecessorGatewaySha256,
    targetGatewaySha256,
    predecessorNodeIntelligenceDescriptorSha256,
    targetNodeIntelligenceDescriptorSha256,
    untouched: [
      "live L2 graphs",
      "n8n workflow data",
      "L1",
      "Engine UI",
      "node-intelligence image",
      "databases",
      "credentials",
      "MCP Nginx",
      "embedded AI Workspace",
    ],
  }, null, 2));
} finally {
  await rm(stage, { recursive: true, force: true });
}

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

  const gatewaySource = await readFile(
    join(engineRoot, "nodedc-source/server/routes/engineAgentGateway.js"),
    "utf8",
  );
  for (const marker of [
    "const ENGINE_AGENT_MCP_VERSION = '0.9.0'",
    "name: 'engine_plan_l2_execution_plan_materialization'",
    "name: 'engine_apply_l2_execution_plan_materialization'",
    "'execution-plan.apply'",
  ]) {
    if (!gatewaySource.includes(marker)) {
      throw new Error(
        `engine_mcp_execution_plan_materialization_gateway_marker_missing:${marker}`,
      );
    }
  }
  if (/gelios|robot2b/i.test(
    await readFile(
      join(engineRoot, "nodedc-source/server/l2ExecutionPlan/compiler.js"),
      "utf8",
    ),
  )) {
    throw new Error("engine_mcp_execution_plan_materialization_provider_hardcode");
  }

  const descriptor = JSON.parse(
    await readFile(join(engineRoot, descriptorPath), "utf8"),
  );
  if (
    descriptor?.schemaVersion !== "nodedc.engine.deploy-transition/v1"
    || descriptor?.id !== "engine-mcp-l2-execution-plan-materialization-v1"
    || descriptor?.mcpVersion !== "0.9.0"
    || descriptor?.plan?.tool
      !== "engine_plan_l2_execution_plan_materialization"
    || descriptor?.apply?.tool
      !== "engine_apply_l2_execution_plan_materialization"
    || descriptor?.providerLogicAuthority !== "trusted-provider-package"
    || descriptor?.unmanagedGraphPolicy !== "explicit-adoption-required"
    || descriptor?.embeddedCodexChanged !== false
  ) {
    throw new Error(
      "engine_mcp_execution_plan_materialization_descriptor_contract_mismatch",
    );
  }
}

async function buildTargetNodeIntelligenceDescriptor() {
  const baselineBytes = await readFile(baselineArtifact);
  if (digest(baselineBytes) !== baselineArtifactSha256) {
    throw new Error(
      "engine_mcp_execution_plan_materialization_baseline_artifact_mismatch",
    );
  }
  const baseline = run("tar", [
    "-xOf",
    baselineArtifact,
    `payload/${nodeIntelligenceDescriptorPath}`,
  ]).stdout;
  if (
    digest(Buffer.from(baseline, "utf8"))
    !== predecessorNodeIntelligenceDescriptorSha256
  ) {
    throw new Error(
      "engine_mcp_execution_plan_materialization_baseline_descriptor_mismatch",
    );
  }
  const descriptor = JSON.parse(baseline);
  if (
    descriptor?.schemaVersion
      !== "nodedc.engine-node-intelligence-transition/v1"
    || descriptor?.action !== "activate"
    || descriptor?.releaseId !== "2.33.2-974a9fb3492f"
    || descriptor?.source?.gatewaySha256 !== predecessorGatewaySha256
  ) {
    throw new Error(
      "engine_mcp_execution_plan_materialization_baseline_contract_mismatch",
    );
  }
  descriptor.source.gatewaySha256 = targetGatewaySha256;
  const rendered = `${JSON.stringify(descriptor, null, 2)}\n`;
  if (
    digest(Buffer.from(rendered, "utf8"))
    !== targetNodeIntelligenceDescriptorSha256
  ) {
    throw new Error(
      "engine_mcp_execution_plan_materialization_target_descriptor_mismatch",
    );
  }
  return rendered;
}

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: "utf8",
    maxBuffer: 128 * 1024 * 1024,
    stdio: ["ignore", "pipe", "pipe"],
  });
  if (result.status !== 0) {
    throw new Error(`${command}_failed:${result.stderr || result.stdout}`);
  }
  return result;
}
