import { describe, it, expect } from 'vitest'
import {
  aggregateByDepartment,
  news2AcuityTier,
  normalizeDepartmentKey,
  summarizeDepartments,
} from '@/composables/departmentFormat'

const encounters = [
  {
    department: 'ICU',
    news2Score: 8,
    openAlertCount: 2,
    sepsisActive: true,
    sepsisBundleStatus: 'IN_PROGRESS',
  },
  {
    department: 'Icu',
    news2Score: 5,
    openAlertCount: 1,
    sepsisActive: false,
    sepsisBundleStatus: null,
  },
  {
    department: 'GENERAL_MEDICINE',
    news2Score: 2,
    openAlertCount: 0,
    sepsisActive: false,
    sepsisBundleStatus: null,
  },
  {
    department: 'Surgery',
    news2Score: 6,
    openAlertCount: 3,
    sepsisActive: true,
    sepsisBundleStatus: 'IN_PROGRESS',
  },
]

describe('departmentFormat', () => {
  it('normalizesDepartmentKeys', () => {
    expect(normalizeDepartmentKey('Icu')).toBe('ICU')
    expect(normalizeDepartmentKey('GeneralMedicine')).toBe('GENERAL_MEDICINE')
  })

  it('classifiesNews2Acuity', () => {
    expect(news2AcuityTier(2)).toBe('low')
    expect(news2AcuityTier(5)).toBe('medium')
    expect(news2AcuityTier(7)).toBe('high')
  })

  it('aggregatesByDepartment', () => {
    const departments = aggregateByDepartment(encounters, [
      { department: 'ICU', total: 10 },
      { department: 'SURGERY', total: 4 },
    ])

    const icu = departments.find(d => d.key === 'ICU')
    const surgery = departments.find(d => d.key === 'SURGERY')
    const general = departments.find(d => d.key === 'GENERAL_MEDICINE')

    expect(icu.patientCount).toBe(2)
    expect(icu.acuity.high).toBe(1)
    expect(icu.acuity.medium).toBe(1)
    expect(icu.activeBundleCount).toBe(1)
    expect(icu.openAlertCount).toBe(3)
    expect(icu.averageNews2).toBe(6.5)
    expect(icu.alertVolume).toBe(10)

    expect(surgery.patientCount).toBe(1)
    expect(surgery.activeBundleCount).toBe(1)
    expect(surgery.alertVolume).toBe(4)

    expect(general.patientCount).toBe(1)
    expect(general.acuity.low).toBe(1)
  })

  it('summarizesTotals', () => {
    const departments = aggregateByDepartment(encounters)
    expect(summarizeDepartments(departments)).toEqual({
      patientCount: 4,
      criticalCount: 1,
      activeBundleCount: 2,
      openAlertCount: 6,
    })
  })
})
