/**
 * Whether a trip invitation may still be accepted (2026-09-17).
 *
 * Removing a pilot sets their participant row to `removed` and expires their
 * open invitation. Both are checked here so an old link in the pilot's inbox
 * can never flip the row back to `active` — Nash: the inviter "has the full
 * power to remove the relation between this pilot and him".
 *
 * Pure so the rule is testable; the accept route and the landing page ask
 * this one question.
 */
export type InvitationAcceptState = 'ok' | 'expired' | 'already_accepted' | 'withdrawn'

export function invitationAcceptState(
  invitation: { expires_at: string; accepted_at: string | null } | null | undefined,
  participant: { status: string } | null | undefined,
  nowMs: number = Date.now(),
): InvitationAcceptState {
  if (!invitation || new Date(invitation.expires_at).getTime() < nowMs) return 'expired'
  if (participant?.status === 'removed') return 'withdrawn'
  if (invitation.accepted_at) return 'already_accepted'
  return 'ok'
}
