import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest'
import {
  bundleUrgency,
  formatCountdown,
  sortBundlesByUrgency,
  summarizeBundles,
} from '@/composables/sepsisFormat'

const baseBundle = {
  complianceStatus: 'IN_PROGRESS',
  deadlineAt: '2026-06-23T15:00:00Z',
}

describe('sepsisFormat', () => {
  const now = new Date('2026-06-23T14:00:00Z').getTime()

  beforeEach(() => {
    vi.useFakeTimers()
    vi.setSystemTime(now)
  })

  afterEach(() => {
    vi.useRealTimers()
  })

  it('classifiesUrgency', () => {
    expect(bundleUrgency({ ...baseBundle, deadlineAt: '2026-06-23T15:00:00Z' }, now)).toBe('on_track')
    expect(bundleUrgency({ ...baseBundle, deadlineAt: '2026-06-23T14:20:00Z' }, now)).toBe('at_risk')
    expect(bundleUrgency({ ...baseBundle, deadlineAt: '2026-06-23T13:00:00Z' }, now)).toBe('overdue')
    expect(bundleUrgency({ complianceStatus: 'NON_COMPLIANT', deadlineAt: '2026-06-23T15:00:00Z' }, now)).toBe('overdue')
  })

  it('formatsCountdown', () => {
    expect(formatCountdown('2026-06-23T14:45:00Z', now)).toBe('45:00')
    expect(formatCountdown('2026-06-23T13:00:00Z', now)).toBe('0:00')
  })

  it('sortsByUrgency', () => {
    const bundles = [
      { id: 'a', complianceStatus: 'IN_PROGRESS', deadlineAt: '2026-06-23T15:00:00Z' },
      { id: 'b', complianceStatus: 'IN_PROGRESS', deadlineAt: '2026-06-23T13:00:00Z' },
      { id: 'c', complianceStatus: 'IN_PROGRESS', deadlineAt: '2026-06-23T14:20:00Z' },
    ]
    const sorted = sortBundlesByUrgency(bundles, now)
    expect(sorted.map(b => b.id)).toEqual(['b', 'c', 'a'])
  })

  it('summarizesBundles', () => {
    const bundles = [
      { complianceStatus: 'IN_PROGRESS', deadlineAt: '2026-06-23T15:00:00Z' },
      { complianceStatus: 'IN_PROGRESS', deadlineAt: '2026-06-23T14:20:00Z' },
      { complianceStatus: 'IN_PROGRESS', deadlineAt: '2026-06-23T13:00:00Z' },
    ]
    expect(summarizeBundles(bundles, now)).toEqual({ on_track: 1, at_risk: 1, overdue: 1 })
  })
})
