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

vi.mock('vue-chartjs', () => ({
  Chart: {
    name: 'Chart',
    template: '<div class="mock-chart" />',
    props: ['type', 'data', 'options'],
  },
}))

describe('SofaHistory', () => {
  beforeEach(() => {
    window.matchMedia = vi.fn().mockReturnValue({ matches: false })
  })
  const history = [
    {
      totalScore: 4,
      respiratoryScore: 1,
      coagulationScore: 1,
      liverScore: 0,
      cardiovascularScore: 1,
      cnsScore: 1,
      renalScore: 0,
      calculatedAt: '2026-06-23T10:00:00Z',
    },
    {
      totalScore: 8,
      respiratoryScore: 2,
      coagulationScore: 1,
      liverScore: 1,
      cardiovascularScore: 2,
      cnsScore: 1,
      renalScore: 1,
      calculatedAt: '2026-06-23T14:00:00Z',
    },
  ]

  it('rendersTitleAndChart', () => {
    const wrapper = mount(SofaHistory, { props: { history } })
    expect(wrapper.text()).toContain('SOFA Score Over Time')
    expect(wrapper.find('.mock-chart').exists()).toBe(true)
  })

  it('sortsHistoryChronologically', () => {
    const wrapper = mount(SofaHistory, { props: { history: [...history].reverse() } })
    const chart = wrapper.findComponent({ name: 'Chart' })
    const totals = chart.props('data').datasets.find(d => d.label === 'SOFA Total').data
    expect(totals).toEqual([4, 8])
  })

  it('includesOrganSystemDatasets', () => {
    const wrapper = mount(SofaHistory, { props: { history } })
    const chart = wrapper.findComponent({ name: 'Chart' })
    const labels = chart.props('data').datasets.map(d => d.label)
    expect(labels).toContain('Respiratory')
    expect(labels).toContain('Renal')
    expect(labels).toContain('SOFA Total')
  })
})
