#!/usr/bin/env -S npx --yes tsx
// =============================================================================
// list_prs_ready_to_merge.ts — Open PRs approved, up-to-date, and CI passing
//
// 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 NOT stale                    (stale_state == not_stale)
//   • have passing CI                  (ci_status == 'passing')
//
// These PRs are ready to be merged immediately — they are approved, their
// branch is already up-to-date with the base branch, and all CI checks have
// passed.  No rebase, conflict resolution, or CI wait is required.
//
// CI filter rationale: merging a PR with failing or pending CI would push
// broken code to the base branch.  Only 'passing' is accepted here.  PRs
// with 'pending' or 'failing' CI appear in none of the approved buckets
// until their CI resolves, preventing premature merges.
//
// Hard-coded filters (not configurable):
//   state:        'open'          only open PRs are considered
//   minApprovals: 1               at least one non-dismissed approval required
//   stale:        'not_stale'     PR head must be current with its base branch
//   ciStatus:     ['passing']     all CI checks must have succeeded
//
// USAGE:
//   list_prs_ready_to_merge.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:        'not_stale',
    ciStatus:     ['passing'],
  });
  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);
});
