#!/usr/bin/env -S npx --yes tsx
// =============================================================================
// list_prs_addressed_changes_ci_passing.ts — Open PRs with passing CI, zero
//                                             approvals, and addressed
//                                             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 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 active REQUEST_CHANGES review
//     (hasRequestChanges == true)
//     i.e. at least one reviewer's most recent non-dismissed review is
//     REQUEST_CHANGES.
//   • all REQUEST_CHANGES reviews are ADDRESSED
//     (hasUnaddressedRequestChanges == false)
//     i.e. every reviewer whose latest review is REQUEST_CHANGES submitted
//     that review against an older commit — the author has pushed at least
//     one new commit since the review was filed.
//
// "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.
//
// These are the highest-priority PRs for the peer review pipeline: a reviewer
// already invested time analysing the code, the author responded with fixes,
// and the CI confirms the code is healthy — now the reviewer (or a new one)
// needs to verify the feedback was adequately addressed.
//
// 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
//   hasRequestChanges:             true        at least one active REQUEST_CHANGES
//   hasUnaddressedRequestChanges:  false       all REQUEST_CHANGES have been
//                                              followed by new commits
//
// USAGE:
//   list_prs_addressed_changes_ci_passing.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,
    hasRequestChanges:            true,
    hasUnaddressedRequestChanges: 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);
});
