const SUBSCRIPTION_STATUSES = new Set([
  'active',
  'canceled',
  'cancelled',
  'pending',
  'APPROVAL_PENDING',
  'APPROVED',
  'ACTIVE',
  'SUSPENDED',
  'CANCELLED',
  'EXPIRED',
])

export type CreateSubscriptionInput = {
  idempotencyKey: string
  planId: string
  currentPeriod: string
  status?: string
}

export type Subscription = {
  id: string
  status: string
  currentPeriod: string
  planId: string
}

export type ParsedSubscriptionWebhook =
  | {
      kind: 'settlement'
      subscriptionId: string
      period: string
      amountMinor: bigint
      currency: string
      eventId?: string
    }
  | {
      kind: 'other'
      raw: unknown
      eventId?: string
    }

export function validateSubscriptionStatus(status: string): string {
  if (typeof status !== 'string' || !SUBSCRIPTION_STATUSES.has(status)) {
    throw new Error(`unsupported subscription status "${status}"`)
  }
  return status
}
