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

const { mockPush } = vi.hoisted(() => ({
  mockPush: vi.fn(),
}))

vi.mock('vue-router', () => ({
  useRouter: () => ({ push: mockPush }),
}))

const patients = [
  {
    encounterId: 'enc-1',
    firstName: 'Alice',
    lastName: 'A',
    mrn: 'M1',
    roomBed: '101',
    status: 'Active',
    news2Score: 3,
    sofaScore: 4,
    sofaDelta: 0,
    gcsScore: 15,
    qsofaScore: 0,
    attendingPhysician: 'Dr. A',
    admittedAt: '2026-06-21T10:00:00Z',
    lastObservationAt: '2026-06-23T13:00:00Z',
    sepsisActive: false,
    openAlertCount: 0,
  },
  {
    encounterId: 'enc-2',
    firstName: 'Bob',
    lastName: 'B',
    mrn: 'M2',
    roomBed: '102',
    status: 'Active',
    news2Score: 5,
    sofaScore: 6,
    sofaDelta: 1,
    gcsScore: 12,
    qsofaScore: 1,
    attendingPhysician: 'Dr. B',
    admittedAt: '2026-06-22T10:00:00Z',
    lastObservationAt: '2026-06-23T10:00:00Z',
    sepsisActive: false,
    openAlertCount: 1,
  },
  {
    encounterId: 'enc-3',
    firstName: 'Carol',
    lastName: 'C',
    mrn: 'M3',
    roomBed: '103',
    status: 'Active',
    news2Score: 8,
    sofaScore: 10,
    sofaDelta: 2,
    gcsScore: 8,
    qsofaScore: 2,
    attendingPhysician: 'Dr. C',
    admittedAt: '2026-06-20T10:00:00Z',
    lastObservationAt: '2026-06-23T08:00:00Z',
    sepsisActive: true,
    openAlertCount: 3,
  },
]

describe('WardTable', () => {
  const sortProps = {
    sortField: 'news2Score',
    sortDirection: 'desc',
  }

  it('rendersAllPatientRows', () => {
    const wrapper = mount(WardTable, { props: { patients, ...sortProps } })
    expect(wrapper.findAll('tbody tr')).toHaveLength(3)
  })

  it('emitsClickWithEncounterId', async () => {
    mockPush.mockClear()
    const wrapper = mount(WardTable, { props: { patients, ...sortProps } })
    await wrapper.findAll('tbody tr')[2].trigger('click')
    expect(mockPush).toHaveBeenCalledWith({
      name: 'PatientDetail',
      params: { encounterId: 'enc-3' },
    })
  })

  it('showsCriticalBadgeForHighNews2', () => {
    const wrapper = mount(WardTable, { props: { patients, ...sortProps } })
    const highRiskRow = wrapper.findAll('tbody tr')[2]
    expect(highRiskRow.html()).toContain('text-severity-critical')
  })

  it('showsExtendedClinicalColumns', () => {
    const wrapper = mount(WardTable, { props: { patients, ...sortProps } })
    const header = wrapper.find('thead').text()
    expect(header).toContain('SOFA')
    expect(header).toContain('GCS')
    expect(header).toContain('Department')
    expect(header).toContain('Last vitals')
    expect(wrapper.text()).toContain('Dr. C')
    expect(wrapper.text()).toContain('Δ+2')
  })

  it('emitsSortWhenHeaderClicked', async () => {
    const wrapper = mount(WardTable, { props: { patients, ...sortProps } })
    const roomHeader = wrapper.findAll('thead button').find(button => button.text().includes('Room'))
    await roomHeader.trigger('click')
    expect(wrapper.emitted('sort')).toEqual([['roomBed']])
  })
})
