import path from "node:path";

import { detectProjectRoot } from "../project/detect-project-root.js";
import { assertInitialized } from "../project/assert-initialized.js";
import { invokePowerShellScript } from "../powershell/invoke-script.js";

export async function runCommand(options) {
  const projectRoot = detectProjectRoot(options.projectRoot);
  assertInitialized(projectRoot);
  const scriptPath = path.join(projectRoot, "tools", "harness", "codex-loop.ps1");
  const args = ["-ProjectRoot", projectRoot];

  if (options.taskFile) {
    args.push("-TaskFile", options.taskFile);
  }

  if (options.runUntilDone) {
    args.push("-RunUntilDone");
  }

  await invokePowerShellScript(scriptPath, args, { cwd: projectRoot });
}
