#!/usr/bin/env node
// Prepares package-derived Docker E2E fixtures for git-style npm installs.
import fs from "node:fs";
import path from "node:path";

const [command, rootArg] = process.argv.slice(2);

function usage() {
  console.error("usage: package-git-fixture.mjs prepare <fixture-root>");
  process.exit(2);
}

function readJson(file) {
  return JSON.parse(fs.readFileSync(file, "utf8"));
}

function writeJson(file, value) {
  fs.writeFileSync(file, `${JSON.stringify(value, null, 2)}\n`);
}

function withoutAiRuntimeDependency(value) {
  if (!Array.isArray(value)) {
    return value;
  }
  const next = value.filter((entry) => entry !== "@openclaw/ai");
  return next.length > 0 ? next : undefined;
}

function ensureDependencyIgnores(root) {
  const gitignorePath = path.join(root, ".gitignore");
  const existing = fs.existsSync(gitignorePath) ? fs.readFileSync(gitignorePath, "utf8") : "";
  const lines = new Set(existing.split(/\r?\n/u));
  const required = ["node_modules", "**/node_modules/", "pnpm-lock.yaml"];
  const missing = required.filter((entry) => !lines.has(entry));
  if (missing.length === 0) {
    return;
  }
  const prefix = existing.length === 0 || existing.endsWith("\n") ? existing : `${existing}\n`;
  fs.writeFileSync(gitignorePath, `${prefix}${missing.join("\n")}\n`);
}

function prepare(root) {
  ensureDependencyIgnores(root);
  const packageJsonPath = path.join(root, "package.json");
  const packageJson = readJson(packageJsonPath);
  const aiRuntimeSource = path.join(root, "node_modules", "@openclaw", "ai");
  const aiRuntimePackageJson = path.join(aiRuntimeSource, "package.json");
  if (!fs.existsSync(aiRuntimePackageJson)) {
    return;
  }

  const aiRuntimeTarget = path.join(root, ".openclaw-fixture", "packages", "ai");
  fs.rmSync(aiRuntimeTarget, { force: true, recursive: true });
  fs.mkdirSync(path.dirname(aiRuntimeTarget), { recursive: true });
  fs.renameSync(aiRuntimeSource, aiRuntimeTarget);

  packageJson.dependencies ??= {};
  packageJson.dependencies["@openclaw/ai"] = "file:.openclaw-fixture/packages/ai";
  packageJson.bundleDependencies = withoutAiRuntimeDependency(packageJson.bundleDependencies);
  packageJson.bundledDependencies = withoutAiRuntimeDependency(packageJson.bundledDependencies);
  if (packageJson.bundleDependencies === undefined) {
    delete packageJson.bundleDependencies;
  }
  if (packageJson.bundledDependencies === undefined) {
    delete packageJson.bundledDependencies;
  }
  writeJson(packageJsonPath, packageJson);
}

if (command !== "prepare" || !rootArg) {
  usage();
}

prepare(path.resolve(rootArg));
