import { describe, it, expect, beforeEach } from 'vitest'
import { mount } from '@vue/test-utils'
import { createPinia, setActivePinia } from 'pinia'
import AlertCard from '@/components/alerts/AlertCard.vue'

const openAlert = {
  id: 'alert-1',
  alertType: 'SepsisWarning',
  severity: 'Critical',
  status: 'Open',
  details: 'SIRS criteria met',
  triggeredAt: '2026-06-19T12:00:00Z',
}

const resolvedAlert = {
  ...openAlert,
  id: 'alert-2',
  status: 'Resolved',
}

describe('AlertCard', () => {
  beforeEach(() => {
    setActivePinia(createPinia())
  })

  it('showsAlertTypeAndSeverity', () => {
    const wrapper = mount(AlertCard, { props: { alert: openAlert } })
    expect(wrapper.text()).toContain('Critical')
    expect(wrapper.text()).toContain('Sepsis Warning (SIRS — Legacy)')
  })

  it('acknowledgeButtonEmitsEvent', async () => {
    const wrapper = mount(AlertCard, { props: { alert: openAlert } })
    const ackButton = wrapper.findAll('button').find(b => b.text() === 'Acknowledge')
    await ackButton.trigger('click')
    expect(wrapper.emitted('acknowledge')).toHaveLength(1)
  })

  it('resolvedAlertHidesActions', () => {
    const wrapper = mount(AlertCard, { props: { alert: resolvedAlert } })
    const actionButtons = wrapper.findAll('button').filter(b => ['Acknowledge', 'Resolve'].includes(b.text()))
    expect(actionButtons).toHaveLength(0)
    expect(wrapper.text()).toContain('Useful')
  })
})
