import { describe, expect, it } from 'vitest'
import { diffExpiryWarnings } from '@/lib/domain/expiry-recheck'

const w = (permitId: string, kind: 'expired' | 'expiring', message: string) => ({ kind, severity: kind === 'expired' ? 'danger' as const : 'warning' as const, message, permitId, tripId: 't1' })

describe('diffExpiryWarnings — daily expiry re-check', () => {
  it('adds an alert for a permit that has become expired', () => {
    const { add, removeIds } = diffExpiryWarnings([], [w('p1', 'expired', 'MS: permit appears expired (2026-10-03).')])
    expect(add).toHaveLength(1); expect(removeIds).toEqual([])
  })
  it('replaces "expiring" with "expired" once the date passes', () => {
    const { add, removeIds } = diffExpiryWarnings(
      [{ id: 'w1', permit_id: 'p1', kind: 'expiring', message: 'IL: permit expires soon (2026-10-07).' }],
      [w('p1', 'expired', 'IL: permit appears expired (2026-10-07).')])
    expect(add.map((a) => a.kind)).toEqual(['expired']); expect(removeIds).toEqual(['w1'])
  })
  it('keeps an alert that still applies, so its acknowledgement survives', () => {
    const msg = 'SC: permit appears expired (2026-02-03).'
    const { add, removeIds } = diffExpiryWarnings([{ id: 'w2', permit_id: 'p2', kind: 'expired', message: msg }], [w('p2', 'expired', msg)])
    expect(add).toEqual([]); expect(removeIds).toEqual([])
  })
  it('removes an alert that no longer applies, for example after a new valid permit for the state', () => {
    const { removeIds } = diffExpiryWarnings([{ id: 'w3', permit_id: 'p3', kind: 'expired', message: 'AL: permit appears expired (2026-10-05).' }], [])
    expect(removeIds).toEqual(['w3'])
  })
})
