import { describe, it, expect } from 'vitest'
import {
  alertSummaryText,
  elevatedContributors,
  formatDuration,
  formatMedicationContext,
  formatTrendLine,
  hasExplanation,
} from '@/composables/alertExplanation'

describe('alertExplanation', () => {
  it('prefersNarrativeSummaryOverDetails', () => {
    const alert = {
      details: 'NEWS2 score 8 (HIGH): ...',
      explanation: {
        narrativeSummary: 'NEWS2 8 — respiratory rate (+3), SpO2 (+2).',
      },
    }
    expect(alertSummaryText(alert)).toBe('NEWS2 8 — respiratory rate (+3), SpO2 (+2).')
  })

  it('fallsBackToDetailsWhenExplanationMissing', () => {
    expect(alertSummaryText({ details: 'Legacy details' })).toBe('Legacy details')
    expect(alertSummaryText({})).toBe('')
  })

  it('filtersElevatedContributors', () => {
    const explanation = {
      scoreContributors: [
        { parameter: 'Respiratory Rate', points: 3, rawValue: '28', normalRange: '12-20' },
        { parameter: 'Heart Rate', points: 0, rawValue: '72', normalRange: '51-90' },
      ],
    }
    expect(elevatedContributors(explanation)).toHaveLength(1)
    expect(elevatedContributors(explanation)[0].parameter).toBe('Respiratory Rate')
  })

  it('formatsTrendAndMedicationContext', () => {
    expect(formatDuration('02:00:00')).toBe('2 hours')
    expect(formatTrendLine({
      parameter: 'Respiratory Rate',
      percentChange: 37,
      duration: '02:00:00',
      direction: 'Increasing',
    })).toContain('Respiratory Rate')
    expect(formatMedicationContext({
      drugName: 'acetaminophen',
      dose: '1000mg',
      route: 'PO',
      administeredAt: '2026-06-22T10:15:00Z',
      relevanceNote: 'Antipyretic — may affect temperature trend',
    })).toContain('acetaminophen')
  })

  it('detectsStructuredExplanation', () => {
    expect(hasExplanation({ explanation: { narrativeSummary: 'x' } })).toBe(true)
    expect(hasExplanation({ details: 'only details' })).toBe(false)
  })
})
