import { describe, expect, it } from 'vitest'
import {
  STATE_INFO_TOPIC_KEYS, STATE_INFO_TOPIC_META, adjacentTopic, isTermList, topicMeta, topicRuleCount,
} from '@/lib/domain/state-info'
import { getStateProvisions, hasStateSheet } from '@/lib/demo/state-info-data'

/**
 * State provisions for the Carrier Dispatch trip view (Nash, 2026-09-25:
 * "currently the design we have on the HHA of state info is not easy to
 * navigate and read"). The numbers are the reason anyone opens this, so the
 * shape that keeps a condition attached to its requirement is pinned here.
 */
describe('topics', () => {
  it('keeps the provider\'s order and answers what each one is for', () => {
    expect(STATE_INFO_TOPIC_KEYS).toEqual(['travel', 'escort', 'permit_limits', 'legal_limits', 'signs', 'superloads'])
    expect(STATE_INFO_TOPIC_META.every((t) => t.label && t.blurb)).toBe(true)
    expect(topicMeta('escort').label).toBe('Escorts')
  })
  it('walks forward and back, and stops at both ends', () => {
    expect(adjacentTopic('travel', -1)).toBeNull()
    expect(adjacentTopic('travel', 1)).toBe('escort')
    expect(adjacentTopic('superloads', 1)).toBeNull()
    expect(adjacentTopic('superloads', -1)).toBe('signs')
  })
})

describe('section shape', () => {
  it('reads as two columns only when every item names what it applies to', () => {
    expect(isTermList({ items: [{ term: 'Over 12′ wide', detail: '1 escort.' }] })).toBe(true)
    expect(isTermList({ items: [{ term: 'A', detail: 'x' }, { detail: 'y' }] })).toBe(false)
    expect(isTermList({ items: [] })).toBe(false)
  })
  it('counts the rules in a topic, so an empty one is visible before it is opened', () => {
    expect(topicRuleCount(undefined)).toBe(0)
    expect(topicRuleCount([{ items: [{ detail: 'a' }] }, { items: [{ detail: 'b' }, { detail: 'c' }] }])).toBe(3)
  })
})

describe('West Virginia, from the provider\'s own sheet', () => {
  const wv = getStateProvisions('wv')
  it('is a real sheet, not the generic fallback', () => {
    expect(hasStateSheet('WV')).toBe(true)
    expect(hasStateSheet('ZZ')).toBe(false)
    expect(getStateProvisions('ZZ')).toEqual(getStateProvisions('QQ'))
  })
  it('keeps every escort threshold attached to its escort count', () => {
    const divided = wv.escort.find((s) => s.heading?.includes('4 or more'))!
    expect(isTermList(divided)).toBe(true)
    expect(divided.items).toContainEqual({ term: 'Over 14′ wide', detail: '2 escorts.' })
    expect(divided.items).toContainEqual({ term: 'Over 15′ high', detail: '1 height pole escort, in addition to any other escort.' })
    const twoLane = wv.escort.find((s) => s.heading?.includes('2-lane'))!
    expect(twoLane.items).toContainEqual({ term: 'At 75′ long', detail: '1 rear escort.' })
  })
  it('separates the two overweight-only night rules, which differ only by weight', () => {
    const ow = wv.travel.find((s) => s.heading === 'Overweight only')!
    expect(ow.items.map((i) => i.term)).toEqual(['Under 110,000 lbs gross', 'Over 110,000 lbs gross'])
    expect(ow.items[0].detail).toMatch(/24\/7/)
    expect(ow.items[1].detail).toMatch(/Monday through Friday/)
  })
  it('carries the permit limits a dispatcher checks a load against', () => {
    const limits = wv.permit_limits[0]
    expect(limits.items).toContainEqual({ term: 'Width', detail: '16′ on 4-lanes, 15′ on 2-lanes.' })
    expect(limits.items.find((i) => i.term === 'Weight')!.detail).toMatch(/120,000/)
  })
  it('has something to show in every topic', () => {
    for (const key of STATE_INFO_TOPIC_KEYS) expect(topicRuleCount(wv[key])).toBeGreaterThan(0)
  })
})
