import { describe, expect, it } from 'vitest'
import { TRIP_NUMBER_RANGES, isTripNumberInRange, numberEnvironment, randomTripNumber, tripNumberDigits } from '@/lib/domain/refcode'

/** Ranges agreed with the Synchron CTO, 2026-10-06. */
describe('trip and order number ranges', () => {
  it('production draws from 200000000–999999999 only', () => {
    for (let i = 0; i < 2000; i++) {
      const n = Number(randomTripNumber('production'))
      expect(n).toBeGreaterThanOrEqual(200_000_000)
      expect(n).toBeLessThanOrEqual(999_999_999)
    }
  })
  it('staging draws from 100000000–100999999 only', () => {
    for (let i = 0; i < 2000; i++) {
      const n = Number(randomTripNumber('staging'))
      expect(n).toBeGreaterThanOrEqual(100_000_000)
      expect(n).toBeLessThanOrEqual(100_999_999)
    }
  })
  it('the two ranges never overlap', () => {
    expect(TRIP_NUMBER_RANGES.staging.max).toBeLessThan(TRIP_NUMBER_RANGES.production.min)
  })
  it('only HHA_ENVIRONMENT=staging selects staging', () => {
    expect(numberEnvironment('staging')).toBe('staging')
    expect(numberEnvironment(undefined)).toBe('production')
    expect(numberEnvironment('production')).toBe('production')
  })
  it('checks a number against its environment', () => {
    expect(isTripNumberInRange('878851361', 'production')).toBe(true)
    expect(isTripNumberInRange('150000000', 'production')).toBe(false)
    expect(isTripNumberInRange('100500000', 'staging')).toBe(true)
    expect(isTripNumberInRange('38879104', 'production')).toBe(false)
  })
  it('reads the 9 digits of a Workspace reference, never an imported one', () => {
    expect(tripNumberDigits('HH-878851361')).toBe('878851361')
    expect(tripNumberDigits('HH-38879104')).toBeNull()
  })
})
