#!/usr/bin/env -S npx --yes tsx
// =============================================================================
// list_prs_stale_clean.ts — Open PRs approved, behind base, no conflicts
//
// Part of: .opencode/skills/auto-agents-system/scripts/
//
// Delegates to list_prs.ts with hard-coded filters.
//
// Returns all OPEN pull requests that:
//   • have at least 1 review approval                     (approvals_count >= 1)
//   • are stale but have NO merge conflicts               (stale_state == stale_no_conflicts)
//     i.e. the base branch has advanced since the PR was last synced,
//     but rebasing/merging would succeed without any conflict resolution.
//
// These PRs are approved and almost ready — they just need to be brought
// up-to-date with their base branch before merging.  The clean conflict state
// means this can be done automatically:
//
//   POST /repos/{owner}/{repo}/pulls/{index}/update?style=rebase
//
// Hard-coded filters (not configurable):
//   state: 'open'                  only open PRs are considered
//   minApprovals: 1                at least one non-dismissed approval required
//   stale: 'stale_no_conflicts'    PR is behind base but conflicts-free
//
// USAGE:
//   list_prs_stale_clean.ts --url URL --pat TOKEN --owner OWNER --repo REPO
//
// OUTPUT (stdout): JSON array — same schema as list_prs.ts
// DEPENDENCIES: Node.js >= 18 (native fetch), npx tsx (for TypeScript execution)
// =============================================================================
import { listPRs, parseConnectionArgs } from './list_prs.ts';

async function main(): Promise<void> {
  const config = parseConnectionArgs(process.argv);
  const prs    = await listPRs(config, {
    state:        'open',
    minApprovals: 1,
    stale:        'stale_no_conflicts',
  });
  process.stdout.write(JSON.stringify(prs, null, 2) + '\n');
}

main().catch((err: unknown) => {
  process.stderr.write(`ERROR: ${err instanceof Error ? err.message : String(err)}\n`);
  process.exit(1);
});
