import { describe, it, expect, beforeEach } from 'vitest'
import {
  defaultSortDirection,
  sortEncounters,
} from '@/composables/wardSort'

const patients = [
  {
    encounterId: '1',
    firstName: 'Bob',
    lastName: 'B',
    roomBed: '202',
    department: 'SURGERY',
    news2Score: 5,
    qsofaScore: 1,
    sepsisActive: false,
    openAlertCount: 1,
  },
  {
    encounterId: '2',
    firstName: 'Alice',
    lastName: 'A',
    roomBed: '101',
    department: 'ICU',
    news2Score: 8,
    qsofaScore: 2,
    sepsisActive: true,
    sepsisBundleStatus: 'IN_PROGRESS',
    openAlertCount: 3,
  },
  {
    encounterId: '3',
    firstName: 'Carol',
    lastName: 'C',
    roomBed: '103',
    department: 'GENERAL_MEDICINE',
    news2Score: 2,
    qsofaScore: 0,
    sepsisActive: false,
    openAlertCount: 0,
  },
]

describe('wardSort', () => {
  beforeEach(() => {
    localStorage.clear()
  })

  it('defaultsTextFieldsToAscending', () => {
    expect(defaultSortDirection('name')).toBe('asc')
    expect(defaultSortDirection('news2Score')).toBe('desc')
  })

  it('sortsByNews2DescendingByDefault', () => {
    const sorted = sortEncounters(patients, 'news2Score', 'desc')
    expect(sorted.map(p => p.encounterId)).toEqual(['2', '1', '3'])
  })

  it('sortsByPatientNameAscending', () => {
    const sorted = sortEncounters(patients, 'name', 'asc')
    expect(sorted.map(p => p.firstName)).toEqual(['Alice', 'Bob', 'Carol'])
  })

  it('sortsByRoomBedAscending', () => {
    const sorted = sortEncounters(patients, 'roomBed', 'asc')
    expect(sorted.map(p => p.roomBed)).toEqual(['101', '103', '202'])
  })

  it('sortsByOpenAlertCountDescending', () => {
    const sorted = sortEncounters(patients, 'openAlertCount', 'desc')
    expect(sorted.map(p => p.encounterId)).toEqual(['2', '1', '3'])
  })

  it('sortsBySepsisStatusDescending', () => {
    const sorted = sortEncounters(patients, 'sepsis', 'desc')
    expect(sorted[0].encounterId).toBe('2')
  })
})
