/**
 * HELD-OUT cell H1-vuln — 3rd domain (security research bug-bounty grant disbursement).
 * NOT payments-marketplace (the oracle donor domain, multideal buyer/vendor) and NOT referral (C09 donor).
 * SELF-DEAL: a reviewer approves a bounty payout to a researcher who is themselves (reviewerUserId ≡ researcherUserId).
 *
 * Uses the oracle recognition lexicon ON PURPOSE: principals end in *UserId; the value sink is an object
 * literal with an amount-like field + a *UserId beneficiary + a string entryType.
 * Asymmetry = INTER-SINK GAP: approveAndPayBounty mediates the self-deal; the sibling autoSettleBounty
 * writes the SAME payout sink but never references the reviewer principal -> incomplete mediation.
 * PREDICTION: oracle FIRES (INTER-SINK GAP) -> domain generalization holds given the naming convention.
 * Self-contained (no imports) so the oracle resolves every callee in-file -> zero unresolved imports.
 */
type Submission = { researcherUserId: string; bountyAgorot: number };

function loadSubmission(id: string): Submission {
  return { researcherUserId: id, bountyAgorot: 0 };
}
function recordLedgerEntry(entry: { amount: number; researcherUserId: string; entryType: string }): void {
  void entry; // persists a ledger row (sink)
}
function isSameUser(a: string, b: string): boolean {
  return a === b;
}

// Manual review path: a reviewer approves a submission, then the payout is recorded.
export function approveAndPayBounty(submissionId: string, reviewerUserId: string): void {
  const submission = loadSubmission(submissionId);
  const researcherUserId = submission.researcherUserId;
  const payoutAmount = submission.bountyAgorot;

  // self-deal guard: a reviewer may not approve a bounty to their own account.
  if (isSameUser(reviewerUserId, researcherUserId)) {
    throw new Error('reviewer cannot approve own submission');
  }

  recordLedgerEntry({
    amount: payoutAmount,
    researcherUserId,
    entryType: 'bounty_payout',
  });
}

// Auto-settle path: a scheduled job settles bounties past SLA. SAME payout sink, but it never loads or
// checks the reviewer -> a researcher who is also the reviewer of record is paid with no self-deal mediation.
export function autoSettleBounty(submissionId: string): void {
  const submission = loadSubmission(submissionId);
  const researcherUserId = submission.researcherUserId;
  const bountyAmount = submission.bountyAgorot;

  recordLedgerEntry({
    amount: bountyAmount,
    researcherUserId,
    entryType: 'bounty_payout',
  });
}
