#!/usr/bin/env -S npx --yes tsx
// =============================================================================
// list_prs_changes_requested.ts — Open PRs with passing CI, no approvals, and
//                                  at least one UNADDRESSED REQUEST_CHANGES review
//
// Part of: .opencode/skills/auto-agents-system/scripts/
//
// Delegates to list_prs.ts with hard-coded filters.
//
// Returns all OPEN pull requests that:
//   • have passing CI                                    (ci_status == 'passing')
//     i.e. all commit status checks have succeeded 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 at least one UNADDRESSED REQUEST_CHANGES review
//     (hasUnaddressedRequestChanges == true)
//     i.e. at least one reviewer's most recent non-dismissed review is
//     REQUEST_CHANGES AND was submitted against the PR's current head commit —
//     meaning no new commits have been pushed to the branch since that review.
//     The author has not yet responded to the reviewer's feedback with code.
//
// "Addressed" vs "unaddressed":
//   A REQUEST_CHANGES review is ADDRESSED when the author subsequently pushes
//   at least one new commit after the review was submitted.  This is detected
//   by comparing review.commit_id (the commit SHA at review time) with the
//   PR's current head SHA — if they differ, commits exist after the review.
//   An UNADDRESSED review still targets the current HEAD: review.commit_id
//   equals the PR's head SHA, meaning no code has changed since the request.
//
// PRs where all REQUEST_CHANGES reviews have been followed by commits are
// excluded — the author has responded and the reviewers need to re-evaluate.
// PRs with pending or failing CI are excluded.  PRs with at least one active
// approval are also excluded.
//
// Hard-coded filters (not configurable):
//   state:                         'open'      only open PRs are considered
//   ciStatus:                      ['passing'] all CI checks must have succeeded
//   maxApprovals:                  0           no active approvals from any reviewer
//   hasUnaddressedRequestChanges:  true        at least one REQUEST_CHANGES not yet
//                                              followed by new commits
//
// USAGE:
//   list_prs_changes_requested.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:                     ['passing'],
    maxApprovals:                 0,
    hasUnaddressedRequestChanges: true,
  });
  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);
});
