import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest'
import { mount } from '@vue/test-utils'
import EncounterTimeline from '@/components/patient/EncounterTimeline.vue'

const events = [
  {
    type: 'observation',
    timestamp: '2026-06-23T10:00:00Z',
    observationCode: 'TEMP_C',
    value: 38.8,
    unit: 'C',
  },
  {
    type: 'alert',
    timestamp: '2026-06-23T11:00:00Z',
    alertType: 'QSOFA_SCREEN',
    severity: 'WARNING',
    status: 'OPEN',
  },
  {
    type: 'medication',
    timestamp: '2026-06-23T12:00:00Z',
    drugName: 'Ceftriaxone',
    dose: 1,
    doseUnit: 'g',
    route: 'IV',
  },
]

describe('EncounterTimeline', () => {
  beforeEach(() => {
    vi.useFakeTimers()
    vi.setSystemTime(new Date('2026-06-23T14:00:00Z'))
  })

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

  it('rendersEvents', () => {
    const wrapper = mount(EncounterTimeline, { props: { events } })
    expect(wrapper.text()).toContain('Encounter Timeline')
    expect(wrapper.text()).toContain('Ceftriaxone')
    expect(wrapper.text()).toContain('qSOFA Screen')
  })

  it('filtersByEventType', async () => {
    const wrapper = mount(EncounterTimeline, { props: { events } })
    const medToggle = wrapper.findAll('button').find(b => b.text() === 'Medications')
    await medToggle.trigger('click')
    expect(wrapper.text()).not.toContain('Ceftriaxone')
    expect(wrapper.text()).toContain('Temperature')
  })

  it('collapsesWhenHeaderClicked', async () => {
    const wrapper = mount(EncounterTimeline, { props: { events } })
    await wrapper.find('button').trigger('click')
    expect(wrapper.text()).not.toContain('Ceftriaxone')
  })
})
