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

const sepsisAlert = {
  id: 'alert-1',
  alertType: 'SEPSIS_WARNING',
  severity: 'CRITICAL',
  details: 'SIRS criteria met',
  triggeredAt: '2026-06-19T12:00:00Z',
}

const qsofaScreenAlert = {
  id: 'alert-2',
  alertType: 'QSOFA_SCREEN',
  severity: 'WARNING',
  details: 'qSOFA screen positive',
  triggeredAt: '2026-06-19T12:30:00Z',
}

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

  it('showsExplanationForLegacySepsisWarning', () => {
    const wrapper = mount(AlertReasoning, { props: { alert: sepsisAlert } })
    expect(wrapper.text()).toContain('SIRS / Sepsis Alert (Legacy)')
    expect(wrapper.text()).toContain('Historical alert')
    expect(wrapper.text()).toContain('SOFA delta')
  })

  it('showsExplanationForQsofaScreen', () => {
    const wrapper = mount(AlertReasoning, { props: { alert: qsofaScreenAlert } })
    expect(wrapper.text()).toContain('qSOFA Screen')
    expect(wrapper.text()).toContain('Bedside screen positive')
    expect(wrapper.text()).toContain('Recommend ordering SOFA labs (PaO₂')
  })

  it('showsRawDetailsForUnknownType', () => {
    const unknown = {
      id: 'alert-3',
      alertType: 'CustomUnknown',
      severity: 'WARNING',
      details: 'Something unusual happened',
      triggeredAt: '2026-06-19T13:00:00Z',
    }
    const wrapper = mount(AlertReasoning, { props: { alert: unknown } })
    expect(wrapper.text()).toContain('CustomUnknown')
    expect(wrapper.text()).toContain('Something unusual happened')
  })

  it('showsStructuredExplanationWhenPresent', () => {
    const news2Alert = {
      id: 'alert-4',
      alertType: 'NEWS2_EMERGENCY',
      severity: 'CRITICAL',
      details: 'NEWS2 score 8 (HIGH): RESP_RATE=3, SPO2=2',
      triggeredAt: '2026-06-19T14:00:00Z',
      explanation: {
        narrativeSummary: 'NEWS2 8 — respiratory rate (+3), SpO2 (+2).',
        scoreContributors: [
          { parameter: 'Respiratory Rate', points: 3, rawValue: '28', normalRange: '12-20' },
          { parameter: 'SpO2', points: 2, rawValue: '92%', normalRange: '96-100%' },
        ],
        trend: {
          parameter: 'Respiratory Rate',
          percentChange: 37,
          duration: '02:00:00',
          direction: 'Increasing',
        },
        medicationContext: {
          drugName: 'acetaminophen',
          dose: '1000mg',
          route: 'PO',
          administeredAt: '2026-06-19T13:30:00Z',
          relevanceNote: 'Antipyretic — may affect temperature trend',
        },
      },
    }

    const wrapper = mount(AlertReasoning, { props: { alert: news2Alert } })
    expect(wrapper.text()).toContain('NEWS2 8 — respiratory rate (+3), SpO2 (+2).')
    expect(wrapper.text()).toContain('Respiratory Rate')
    expect(wrapper.text()).toContain('Correlated medication')
    expect(wrapper.text()).toContain('acetaminophen')
    expect(wrapper.text()).toContain('increasing 37% over 2 hours')
  })
})