#!/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 sourceRoot = resolve(here, "ops-mcp-workspace-tools");
const overlayRoot = join(sourceRoot, "overlays");
const release = JSON.parse(await readFile(join(sourceRoot, "release.json"), "utf8"));
const artifactRoot = resolve(
  process.env.NODEDC_DEPLOY_ARTIFACT_DIR || resolve(here, "../deploy-artifacts"),
);
const [requestedRelease = release.release, ...extra] = process.argv.slice(2);

if (extra.length || requestedRelease !== release.release) {
  throw new Error("usage: build-ops-mcp-workspace-tools-artifacts.mjs [" + release.release + "]");
}

const productionRoots = {
  tasker: "/volume1/docker/nodedc-platform/tasker",
  "ops-agents": "/volume1/docker/nodedc-platform/ops-agents",
};
const results = [];

await mkdir(artifactRoot, { recursive: true });
for (const [component, descriptor] of Object.entries(release.components)) {
  validateDescriptor(component, descriptor);
  const target = join(artifactRoot, "nodedc-" + descriptor.artifactId + ".tgz");
  const predecessorPath = join(artifactRoot, "nodedc-" + descriptor.artifactId + ".predecessor.sha256");
  const newPathsPath = join(artifactRoot, "nodedc-" + descriptor.artifactId + ".new-paths");
  await assertFresh(target);
  await assertFresh(predecessorPath);
  await assertFresh(newPathsPath);

  const stage = await mkdtemp(join(tmpdir(), "nodedc-" + component + "-workspace-tools-"));
  const payload = join(stage, "payload");
  try {
    await mkdir(payload, { recursive: true });
    for (const relativePath of descriptor.files) {
      const source = join(overlayRoot, component, relativePath);
      const info = await lstat(source);
      if (!info.isFile() || info.isSymbolicLink()) {
        throw new Error("invalid_overlay_source:" + component + ":" + relativePath);
      }
      await mkdir(dirname(join(payload, relativePath)), { recursive: true });
      await cp(source, join(payload, relativePath), { force: false });
    }

    await writeFile(
      join(stage, "manifest.env"),
      "id=" + descriptor.artifactId + "\ncomponent=" + component + "\ntype=app-overlay\n",
      "utf8",
    );
    await writeFile(join(stage, "files.txt"), descriptor.files.join("\n") + "\n", "utf8");
    run("python3", ["-c", canonicalTarScript(), target, stage]);

    const productionRoot = productionRoots[component];
    const predecessorLines = Object.entries(descriptor.predecessors)
      .map(([relativePath, digest]) => digest + "  " + productionRoot + "/" + relativePath);
    await writeFile(predecessorPath, predecessorLines.join("\n") + "\n", "utf8");
    await writeFile(
      newPathsPath,
      descriptor.newPaths.map((relativePath) => productionRoot + "/" + relativePath).join("\n") + "\n",
      "utf8",
    );

    results.push({
      component,
      artifactId: descriptor.artifactId,
      artifact: target,
      artifactSha256: sha(await readFile(target)),
      predecessorChecks: predecessorPath,
      predecessorChecksSha256: sha(await readFile(predecessorPath)),
      newPaths: newPathsPath,
      newPathsSha256: sha(await readFile(newPathsPath)),
      files: descriptor.files,
    });
  } finally {
    await rm(stage, { recursive: true, force: true });
  }
}

console.log(JSON.stringify({
  ok: true,
  schemaVersion: release.schemaVersion,
  release: release.release,
  deployOrder: ["tasker", "ops-agents"],
  runnerChanged: false,
  results,
}, null, 2));

function validateDescriptor(component, descriptor) {
  if (!(component in productionRoots)) throw new Error("unsupported_component:" + component);
  if (!/^[A-Za-z0-9._-]{1,96}$/.test(descriptor.artifactId)) {
    throw new Error("invalid_artifact_id:" + component);
  }
  if (!Array.isArray(descriptor.files) || descriptor.files.length === 0) {
    throw new Error("empty_file_list:" + component);
  }
  if (new Set(descriptor.files).size !== descriptor.files.length) {
    throw new Error("duplicate_file:" + component);
  }
  for (const relativePath of descriptor.files) validateRelativePath(relativePath);
  for (const relativePath of Object.keys(descriptor.predecessors)) {
    validateRelativePath(relativePath);
    if (!descriptor.files.includes(relativePath)) {
      throw new Error("predecessor_not_in_files:" + component + ":" + relativePath);
    }
    if (!/^[a-f0-9]{64}$/.test(descriptor.predecessors[relativePath])) {
      throw new Error("invalid_predecessor_sha256:" + component + ":" + relativePath);
    }
  }
  for (const relativePath of descriptor.newPaths) {
    validateRelativePath(relativePath);
    if (!descriptor.files.includes(relativePath)) {
      throw new Error("new_path_not_in_files:" + component + ":" + relativePath);
    }
    if (relativePath in descriptor.predecessors) {
      throw new Error("new_path_has_predecessor:" + component + ":" + relativePath);
    }
  }
  const covered = new Set([...Object.keys(descriptor.predecessors), ...descriptor.newPaths]);
  if (covered.size !== descriptor.files.length) {
    throw new Error("predecessor_partition_incomplete:" + component);
  }
}

function validateRelativePath(value) {
  if (
    typeof value !== "string"
    || !value
    || value.startsWith("/")
    || value.split("/").some((part) => !part || part === "." || part === "..")
  ) {
    throw new Error("unsafe_relative_path:" + value);
  }
}

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

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 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;
}

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