module.exports = async function pipeline(args, flags) {
  const rootDir = process.cwd();
  const { runPipeline } = require("../lib/pipeline");
  const fs = require("fs");

  if (!fs.existsSync(require("path").join(rootDir, "skill-routing-matrix.json"))) {
    console.error("Not a CapInt project — run capint init first.");
    process.exit(1);
  }

  const task = args.filter((a) => !a.startsWith("--")).join(" ").trim() || null;
  const result = runPipeline(rootDir, { task, skipIde: Boolean(flags["skip-ide"]) });

  if (flags.json) {
    console.log(JSON.stringify(result, null, 2));
    if (!result.pass && !flags.force) process.exit(1);
    return;
  }

  console.log("\n## CapInt Pipeline\n");
  result.steps.forEach((step, i) => {
    const mark = step.ok ? "✓" : step.advisory ? "!" : "✗";
    console.log(`[${i + 1}/${result.steps.length}] ${step.id}: ${mark} ${step.summary}`);
  });
  console.log(`\nOverall: ${result.pass ? "PASS" : "FAIL"}`);
  if (!result.pass) {
    console.log("Hint: capint doctor · capint recover --list · capint ide sync");
    if (!flags.force) process.exit(1);
  }
  console.log("");
};
