#!/usr/bin/env -S npx --yes tsx
// =============================================================================
// list_prs_needs_review_stale_clean.ts — Open PRs with no approvals, stale, 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 zero review approvals        (approvals_count == 0)
//   • 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 are PRs that need a first review AND will also need a server-side
// rebase before they can be merged — but no conflict resolution will be
// required.  Once approved, the workflow is:
//
//   rebase_pr  →  (wait for approval if not yet given)  →  merge_pr
//
// Why this bucket exists:
//   Together with list_prs_needs_review_not_stale and
//   list_prs_needs_review_stale_conflicts, these three scripts partition every
//   open PR with zero approvals into three actionable sub-buckets based on
//   what additional work will be required after a reviewer approves:
//     list_prs_needs_review_not_stale       → review only; merge immediately on approval
//     list_prs_needs_review_stale_clean     → review + server-side rebase; no conflicts
//     list_prs_needs_review_stale_conflicts → review + local rebase; conflicts present
//
// Note: PRs with zero approvals and stale_state == stale_unknown are
// transiently absent while Forgejo computes conflict status; they will appear
// here or in stale_conflicts once Forgejo finishes.
//
// Hard-coded filters (not configurable):
//   state:        'open'                  only open PRs are considered
//   maxApprovals: 0                       PRs with no approvals whatsoever
//   stale:        'stale_no_conflicts'    PR is behind base but conflict-free
//
// USAGE:
//   list_prs_needs_review_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',
    maxApprovals: 0,
    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);
});
