import { describe, expect, it } from 'vitest'
import { computePermitWarnings } from '@/lib/domain/warnings'

const load = {
  load_length_in: 1140, // 95'
  load_width_in: 150, // 12'6"
  load_height_in: 162, // 13'6"
  load_weight_lbs: 142_000,
}

const basePermit = {
  id: 'p1',
  state_code: 'TX',
  permit_length_in: 1140,
  permit_width_in: 150,
  permit_height_in: 162,
  permit_weight_lbs: 142_000,
  effective_date: '2026-08-01',
  expiration_date: '2026-12-31',
}

const today = new Date('2026-08-31T12:00:00')

describe('computePermitWarnings', () => {
  it('produces no warnings when permit covers the load', () => {
    expect(computePermitWarnings(load, basePermit, today)).toEqual([])
  })

  it('flags a width mismatch (permit smaller than load)', () => {
    const warnings = computePermitWarnings(
      load,
      { ...basePermit, permit_width_in: 144 }, // 12'0" < 12'6"
      today,
    )
    expect(warnings).toHaveLength(1)
    expect(warnings[0].kind).toBe('dimension_mismatch')
    expect(warnings[0].severity).toBe('danger')
    expect(warnings[0].message).toContain(`12'0"`)
    expect(warnings[0].message).toContain(`12'6"`)
  })

  it('flags weight mismatch with formatted pounds', () => {
    const warnings = computePermitWarnings(load, { ...basePermit, permit_weight_lbs: 120_000 }, today)
    expect(warnings[0].kind).toBe('dimension_mismatch')
    expect(warnings[0].message).toContain('120,000 lbs')
  })

  it('flags expired permits', () => {
    const warnings = computePermitWarnings(load, { ...basePermit, expiration_date: '2026-08-20' }, today)
    expect(warnings.some((w) => w.kind === 'expired' && w.severity === 'danger')).toBe(true)
  })

  it('flags soon-to-expire permits', () => {
    const warnings = computePermitWarnings(load, { ...basePermit, expiration_date: '2026-09-01' }, today)
    expect(warnings.some((w) => w.kind === 'expiring')).toBe(true)
  })

  // Nash (2026-09-04): "this is not a very important alert." Removed
  // permanently in a04be42; src/lib/domain/warnings.ts says so at the point
  // where the check used to live. This test guards the removal — a
  // not-yet-effective permit must produce NO warning of its own.
  it('does not flag not-yet-effective permits', () => {
    const warnings = computePermitWarnings(load, { ...basePermit, effective_date: '2026-09-15' }, today)
    expect(warnings.some((w) => w.message.includes('not effective yet'))).toBe(false)
  })

  it('skips checks when data is missing (no fake warnings)', () => {
    const warnings = computePermitWarnings(
      { load_length_in: null, load_width_in: null, load_height_in: null, load_weight_lbs: null },
      { ...basePermit, permit_width_in: null, effective_date: null, expiration_date: null },
      today,
    )
    expect(warnings).toEqual([])
  })
})
