import { describe, expect, it } from 'vitest'
import { addBusinessHours, alertSendTime, businessMillisBetween, federalHolidays, isWithinBusinessHours } from '@/lib/domain/business-hours'

const H = 3_600_000
const at = (iso: string) => new Date(iso)
const iso = (d: Date) => d.toISOString().replace('.000', '')

describe('federal holidays (observed dates)', () => {
  it('lists the 2026 observed holidays, moving Saturday July 4 to Friday July 3', () => {
    expect([...federalHolidays(2026)].sort()).toEqual([
      '2026-01-01', '2026-01-19', '2026-02-16', '2026-05-25', '2026-06-19', '2026-07-03',
      '2026-09-07', '2026-10-12', '2026-11-11', '2026-11-26', '2026-12-25',
    ])
  })
  it('observes a Saturday New Year\'s Day on December 31 of the year before', () => {
    expect(federalHolidays(2021).has('2021-12-31')).toBe(true)
  })
})

describe('the Extended-route business clock (America/Chicago)', () => {
  it('counts a plain weekday hour', () => {
    // Tue Oct 13 2026, 8:00 AM CDT (UTC-5)
    expect(iso(addBusinessHours(at('2026-10-13T13:00:00Z'), 1))).toBe('2026-10-13T14:00:00Z')
  })
  it('pauses overnight and resumes at 7 AM', () => {
    // Tue 6:30 PM CDT + 1h → 30 min Tuesday, 30 min Wednesday from 7 AM
    expect(iso(addBusinessHours(at('2026-10-13T23:30:00Z'), 1))).toBe('2026-10-14T12:30:00Z')
  })
  it('uses Saturday 9 AM–5 PM', () => {
    // Fri Oct 16 6:00 PM CDT + 2h → 1h Friday, 1h Saturday from 9 AM
    expect(iso(addBusinessHours(at('2026-10-16T23:00:00Z'), 2))).toBe('2026-10-17T15:00:00Z')
  })
  it('skips Sunday', () => {
    // Sat Oct 17 4:30 PM CDT + 1h → 30 min Saturday, Monday 7:30 AM
    expect(iso(addBusinessHours(at('2026-10-17T21:30:00Z'), 1))).toBe('2026-10-19T12:30:00Z')
  })
  it('skips a federal holiday (Columbus Day, Mon Oct 12 2026)', () => {
    // Sat Oct 10 4:00 PM CDT + 2h → 1h Saturday, Sunday and Monday closed, Tuesday 8:00 AM
    expect(iso(addBusinessHours(at('2026-10-10T21:00:00Z'), 2))).toBe('2026-10-13T13:00:00Z')
  })
  it('stays correct across the end of daylight saving (Nov 1 2026)', () => {
    // Sat Oct 31 4:00 PM CDT + 2h → 1h Saturday, Monday Nov 2 8:00 AM CST (UTC-6)
    expect(iso(addBusinessHours(at('2026-10-31T21:00:00Z'), 2))).toBe('2026-11-02T14:00:00Z')
  })
  it('measures elapsed business time between two moments', () => {
    // Fri Oct 16 6:00 PM CDT → Mon Oct 19 8:00 AM CDT = 1h Fri + 8h Sat + 1h Mon
    expect(businessMillisBetween(at('2026-10-16T23:00:00Z'), at('2026-10-19T13:00:00Z'))).toBe(10 * H)
    expect(businessMillisBetween(at('2026-10-19T13:00:00Z'), at('2026-10-16T23:00:00Z'))).toBe(0)
  })
  it('queues an alert due outside business hours for the next opening', () => {
    expect(iso(alertSendTime(at('2026-10-18T17:00:00Z')))).toBe('2026-10-19T12:00:00Z') // Sunday noon → Mon 7 AM
    expect(iso(alertSendTime(at('2026-10-13T15:00:00Z')))).toBe('2026-10-13T15:00:00Z') // Tuesday 10 AM → now
    expect(isWithinBusinessHours(at('2026-10-14T00:00:00Z'))).toBe(true) // Tuesday 7:00 PM, the closing moment
  })
})
