import { describe, it, expect, beforeEach } from 'vitest'
import { createPinia, setActivePinia } from 'pinia'
import { useSettingsStore } from '@/stores/settings'
import { useWardStore } from '@/stores/ward'

describe('ward store sort', () => {
  beforeEach(() => {
    localStorage.clear()
    setActivePinia(createPinia())
  })

  it('persistsSortPreference', () => {
    const settings = useSettingsStore()
    const ward = useWardStore()

    ward.setSort('name')
    expect(settings.wardSortField).toBe('name')
    expect(settings.wardSortDirection).toBe('asc')
    expect(localStorage.getItem('wardSortField')).toBe('name')

    ward.setSort('name')
    expect(settings.wardSortDirection).toBe('desc')
    expect(localStorage.getItem('wardSortDirection')).toBe('desc')
  })
})

describe('ward store filters', () => {
  beforeEach(() => {
    localStorage.clear()
    setActivePinia(createPinia())
  })

  it('filtersEncountersForDisplay', () => {
    const ward = useWardStore()
    ward.encounters = [
      { encounterId: '1', firstName: 'Alice', lastName: 'A', mrn: 'M1', news2Score: 8, openAlertCount: 1 },
      { encounterId: '2', firstName: 'Bob', lastName: 'B', mrn: 'M2', news2Score: 3, openAlertCount: 0 },
    ]

    ward.toggleFilter('critical')
    expect(ward.displayEncounters.map(e => e.encounterId)).toEqual(['1'])

    ward.toggleFilter('hasAlerts')
    expect(ward.displayEncounters.map(e => e.encounterId)).toEqual(['1'])

    ward.clearFilters()
    expect(ward.displayEncounters.map(e => e.encounterId)).toEqual(['1', '2'])
  })
})
