// Door payment tenders.
//
// The door check-in screen offers four one-tap tenders. Each maps onto an
// existing payments.provider so the rest of the app (capacity, sweeps, admin
// payment lists, receipts) keeps working unchanged, while payments.method
// records which tender was actually used for the end-of-night cash-up.
//
// Bitcoin currently maps to the 'lightning' provider but records the payment as
// already made — the same trust model as cash, no invoice generated. When a real
// Lightning flow lands it slots in here: the tender keeps its name and provider,
// only the settlement path in routes/door.ts changes.

export const DOOR_PAYMENT_METHODS = ['cash', 'bitcoin', 'transfer', 'guest'] as const;

export type DoorPaymentMethod = (typeof DOOR_PAYMENT_METHODS)[number];

interface DoorTender {
  /** Existing payments.provider this tender is stored as. */
  provider: 'cash' | 'lightning' | 'bank_transfer';
  /** Human label used in payment references and toasts. */
  label: string;
  /** Comp tenders carry no revenue and always record a zero amount. */
  isComp: boolean;
}

export const DOOR_TENDERS: Record<DoorPaymentMethod, DoorTender> = {
  cash: { provider: 'cash', label: 'cash', isComp: false },
  bitcoin: { provider: 'lightning', label: 'bitcoin', isComp: false },
  transfer: { provider: 'bank_transfer', label: 'transfer', isComp: false },
  guest: { provider: 'cash', label: 'guest', isComp: true },
};

export function isDoorPaymentMethod(value: unknown): value is DoorPaymentMethod {
  return typeof value === 'string' && (DOOR_PAYMENT_METHODS as readonly string[]).includes(value);
}

/** Ticket paymentStatus a tender settles to: comps are 'comp', everything else 'paid'. */
export function paymentStatusForMethod(method: DoorPaymentMethod): 'paid' | 'comp' {
  return DOOR_TENDERS[method].isComp ? 'comp' : 'paid';
}

/** Amount actually recorded: comps are always zero regardless of what was requested. */
export function amountForMethod(method: DoorPaymentMethod, requested: number): number {
  return DOOR_TENDERS[method].isComp ? 0 : Math.max(0, requested);
}

export function doorReference(method: DoorPaymentMethod): string {
  return DOOR_TENDERS[method].isComp
    ? 'Door — guest (comp)'
    : `Door — paid by ${DOOR_TENDERS[method].label}`;
}
