import { describe, expect, it } from 'vitest'
import { ACCESS_MAP, getAccessLevel, hasAnyAccess, hasFullAccess, SUBSCRIPTION_POLICY } from './subscription.policy'

describe('subscription.policy', () => {
  it('maps each known subscription status to the intended access level', () => {
    expect(ACCESS_MAP).toMatchObject({
      TRIALING: 'full',
      ACTIVE: 'full',
      PAYMENT_PENDING: 'full',
      UNPAID: 'full',
      PAST_DUE: 'limited',
      SUSPENDED: 'read_only',
      PAUSED: 'read_only',
      CANCELLED: 'none',
      EXPIRED: 'none',
    })
  })

  it('defaults unknown statuses to no access', () => {
    expect(getAccessLevel('MYSTERY_BOX')).toBe('none')
    expect(hasAnyAccess('MYSTERY_BOX')).toBe(false)
    expect(hasFullAccess('MYSTERY_BOX')).toBe(false)
  })

  it('distinguishes full access from degraded but nonzero access', () => {
    expect(hasFullAccess('ACTIVE')).toBe(true)
    expect(hasAnyAccess('ACTIVE')).toBe(true)

    expect(hasFullAccess('PAST_DUE')).toBe(false)
    expect(hasAnyAccess('PAST_DUE')).toBe(true)

    expect(hasFullAccess('SUSPENDED')).toBe(false)
    expect(hasAnyAccess('SUSPENDED')).toBe(true)

    expect(hasFullAccess('CANCELLED')).toBe(false)
    expect(hasAnyAccess('CANCELLED')).toBe(false)
  })

  it('keeps payment retry policy internally coherent', () => {
    expect(SUBSCRIPTION_POLICY.trial.durationDays).toBeGreaterThan(0)
    expect(SUBSCRIPTION_POLICY.payment.retryScheduleDays).toHaveLength(SUBSCRIPTION_POLICY.payment.maxRetryAttempts)
    expect(SUBSCRIPTION_POLICY.payment.retryScheduleDays).toEqual([...SUBSCRIPTION_POLICY.payment.retryScheduleDays].sort((a, b) => a - b))
    expect(SUBSCRIPTION_POLICY.suspension.cancelAfterDays).toBeGreaterThan(SUBSCRIPTION_POLICY.pastDue.timeoutDays)
  })
})
