/**
 * saas-admin blueprint · wiring seam for `@platform-modules/billing`.
 *
 * Adapter-minimalism (CLAUDE.md §4): suspending a member mid-cycle stamps a proration-refund INTENT
 * — billing's pure value exports (idempotencyKey, toMinorUnits) compose into the admin action and
 * give the cleanup job its at-most-once key.
 *
 * SCOPE (delivery-stack §4.1, "more than the seam is bloat"): the FULL settlement path
 * (settleCharge / refundCharge / confirmRefund — which inject a LedgerSeam, Pattern B: ledger sits
 * ABOVE billing) is the COMMERCE blueprint's headline flow. Re-running it here would be the
 * generic-platform trap, so this blueprint touches only the helpers. LedgerSeam is re-exported as a
 * type to NAME the injection point the commerce blueprint fills.
 */
import { idempotencyKey, toMinorUnits, type LedgerSeam } from '@platform-modules/billing'

/** Where the commerce blueprint injects @platform-modules/ledger's appendEntry (Pattern B). */
export type AdminLedgerSeam = LedgerSeam

export type RefundIntent = {
  /** at-most-once key — reused as the cleanup-job idempotency key */
  key: string
  /** amount in minor units (cents/agorot) — billing never trusts a float at the boundary */
  minorUnits: bigint
}

/**
 * `amountMinor` is ALREADY in integer minor units (cents/agorot) — billing's `toMinorUnits` rejects
 * floats (`M7`: unit normalization from major units is the host/adapter's job, never billing's). A
 * real host computes the proration in minor units before calling this.
 */
export function prorationRefundIntent(tenantId: string, memberId: string, amountMinor: number): RefundIntent {
  return {
    key: idempotencyKey(['refund', tenantId, memberId]),
    minorUnits: toMinorUnits(amountMinor),
  }
}
