#!/usr/bin/env -S npx --yes tsx
// =============================================================================
// list_prs_no_active_review_ci_failing.ts — Open PRs with failing CI, zero
//                                            approvals, and no active
//                                            REQUEST_CHANGES reviews
//
// Part of: .opencode/skills/auto-agents-system/scripts/
//
// Delegates to list_prs.ts with hard-coded filters.
//
// Returns all OPEN pull requests that:
//   • have failing CI                                    (ci_status == 'failing')
//     i.e. at least one commit status check has failed, errored, or warned
//     for the PR's head SHA.
//   • have zero active review approvals                  (approvals_count == 0)
//     i.e. no reviewer's most recent non-dismissed review is APPROVED.
//   • have NO active REQUEST_CHANGES reviews
//     (hasRequestChanges == false)
//     i.e. no reviewer's latest non-dismissed review is REQUEST_CHANGES.
//
// This bucket captures PRs that need a fresh review but also have failing CI.
// It includes:
//   • PRs that have never been reviewed at all and have broken CI
//   • PRs where all previous reviews were COMMENT (non-blocking) or dismissed
//   • PRs where a reviewer who previously requested changes later had their
//     review dismissed or submitted a non-blocking COMMENT
//
// A reviewer can provide code quality feedback on these PRs while noting the
// CI failures.  Per company policy, all CI gates must pass before a PR can be
// approved and merged, so the reviewer should request changes citing the
// failing CI alongside any code-level issues found.
//
// Hard-coded filters (not configurable):
//   state:             'open'      only open PRs are considered
//   ciStatus:          ['failing'] at least one CI check has failed
//   maxApprovals:      0           no active approvals from any reviewer
//   hasRequestChanges: false       no reviewer's latest review is REQUEST_CHANGES
//
// USAGE:
//   list_prs_no_active_review_ci_failing.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',
    ciStatus:          ['failing'],
    maxApprovals:      0,
    hasRequestChanges: false,
  });
  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);
});
