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

const mockFetch = vi.fn()
const mockState = {
  total: null,
  delta: null,
  organs: [],
  hasStaleData: false,
  staleness: null,
  loading: false,
  fetch: mockFetch,
}

vi.mock('@/composables/useSofa', () => ({
  useSofa: () => mockState,
}))

describe('SofaScorePanel', () => {
  beforeEach(() => {
    mockState.total = null
    mockState.delta = null
    mockState.organs = []
    mockState.hasStaleData = false
    mockState.staleness = null
    mockState.loading = false
    mockFetch.mockClear()
  })

  it('showsLabsPending_WhenNoScore', () => {
    const wrapper = mount(SofaScorePanel, {
      props: { encounterId: 'enc-1' },
    })
    expect(wrapper.text()).toContain('Labs pending')
  })

  it('showsOrganBreakdown', () => {
    mockState.total = 8
    mockState.organs = [
      { name: 'Respiratory', score: 2, max: 4 },
      { name: 'Coagulation', score: 1, max: 4 },
      { name: 'Liver', score: 0, max: 4 },
      { name: 'Cardiovascular', score: 3, max: 4 },
      { name: 'CNS', score: 1, max: 4 },
      { name: 'Renal', score: 1, max: 4 },
    ]
    const wrapper = mount(SofaScorePanel, {
      props: { encounterId: 'enc-1' },
    })
    expect(wrapper.text()).toContain('Respiratory')
    expect(wrapper.text()).toContain('Renal')
    expect(wrapper.text()).toContain('8/24')
  })

  it('showsDeltaBadge_WhenDeltaGe2', () => {
    mockState.total = 10
    mockState.delta = 2
    mockState.organs = [{ name: 'CNS', score: 2, max: 4 }]
    const wrapper = mount(SofaScorePanel, {
      props: { encounterId: 'enc-1' },
    })
    expect(wrapper.text()).toContain('+2 from baseline')
  })

  it('showsStalenessWarning', () => {
    mockState.total = 6
    mockState.hasStaleData = true
    mockState.staleness = {
      staleComponents: ['Platelets'],
      missingComponents: ['PaO2'],
      usedSpO2Fallback: true,
    }
    mockState.organs = [{ name: 'Coagulation', score: 1, max: 4 }]
    const wrapper = mount(SofaScorePanel, {
      props: { encounterId: 'enc-1' },
    })
    expect(wrapper.text()).toContain('Missing: PaO2')
    expect(wrapper.text()).toContain('Stale: Platelets')
    expect(wrapper.text()).toContain('SpO₂/FiO₂ proxy')
  })

  it('organScoreColors', () => {
    mockState.total = 3
    mockState.organs = [
      { name: 'Liver', score: 0, max: 4 },
      { name: 'CNS', score: 2, max: 4 },
      { name: 'Renal', score: 4, max: 4 },
    ]
    const wrapper = mount(SofaScorePanel, {
      props: { encounterId: 'enc-1' },
    })
    const badges = wrapper.findAll('span')
    expect(badges.some(b => b.text().includes('0/4'))).toBe(true)
    expect(badges.some(b => b.text().includes('2/4'))).toBe(true)
    expect(badges.some(b => b.text().includes('4/4'))).toBe(true)
  })

  it('usesResponsiveOrganGrid', () => {
    mockState.total = 4
    mockState.organs = [{ name: 'CNS', score: 1, max: 4 }]
    const wrapper = mount(SofaScorePanel, {
      props: { encounterId: 'enc-1' },
    })
    const grid = wrapper.find('.grid')
    expect(grid.classes()).toContain('grid-cols-2')
    expect(grid.classes()).toContain('sm:grid-cols-3')
    expect(grid.classes()).toContain('lg:grid-cols-6')
  })
})