#!/usr/bin/env -S npx --yes tsx
// =============================================================================
// list_prs_stale_conflicts.ts — Open PRs approved, stale, and conflicted
//
// 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 AND have merge conflicts (stale_state == stale_with_conflicts)
//     i.e. the base branch has advanced since the PR was last synced,
//     AND rebasing/merging would produce conflicts that must be resolved.
//
// These PRs have been reviewed and approved but cannot progress without
// manual intervention.  The PR author (or an agent) must:
//   1. Rebase the branch against the latest base branch locally.
//   2. Resolve all merge conflicts.
//   3. Force-push the resolved commits.
//   4. Request re-review if the original approval is now considered stale.
//
// Hard-coded filters (not configurable):
//   state: 'open'                     only open PRs are considered
//   minApprovals: 1                   at least one non-dismissed approval required
//   stale: 'stale_with_conflicts'     PR is behind base and has merge conflicts
//
// USAGE:
//   list_prs_stale_conflicts.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_with_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);
});
