import { execFile } from "child_process";
import { fileURLToPath } from "url";
import { dirname, join } from "path";
import { writeFileSync } from "fs";

const __filename = fileURLToPath(import.meta.url);
const __dirname = dirname(__filename);
const target = join(__dirname, "..", "..", "auto-agents-system", "scripts", "list_prs_needs_review_stale_conflicts.ts");
const outFile = join(process.cwd(), "stale_prs.json");

execFile("npx", ["--yes", "tsx", target, "--url", "https://git.cleverthis.com", "--pat", "060faa5ff13338d510ef808027b6314a2e895c3d", "--owner", "cleveragents", "--repo", "cleveragents-core"], { timeout: 120000, maxBuffer: 50 * 1024 * 1024 }, (err, stdout, stderr) => {
  if (err) { writeFileSync(outFile, JSON.stringify({ error: err.message })); console.error(err.message); process.exit(1); }
  writeFileSync(outFile, stdout);
  console.log("Wrote", stdout.length, "bytes to", outFile);
});
