import { describe, it, expect } from 'vitest'
import { mount } from '@vue/test-utils'
import SessionCard from '@/components/simulation/SessionCard.vue'

const baseSession = {
  id: 'session-b-alert-quality',
  name: 'Session B — Alert quality deep dive',
  goal: 'Compare alert types across contrasting scenarios.',
  estimatedMinutes: 50,
  defaultSpeed: 120,
  scenarios: [
    { id: 'medication-false-alarm-01', name: 'Medication False Alarm' },
    { id: 'uti-sepsis-elderly-01', name: 'UTI Sepsis Elderly' },
    { id: 'respiratory-failure-asthma-01', name: 'Respiratory Failure' },
  ],
}

describe('SessionCard', () => {
  it('rendersPresetSummary', () => {
    const wrapper = mount(SessionCard, {
      props: { session: baseSession },
    })

    expect(wrapper.text()).toContain(baseSession.name)
    expect(wrapper.text()).toContain(baseSession.goal)
    expect(wrapper.text()).toContain('~50 min')
    expect(wrapper.text()).toContain('3 patients')
    expect(wrapper.text()).toContain('Medication False Alarm')
    expect(wrapper.text()).toContain('UTI Sepsis Elderly')
  })

  it('disablesStartWhenInsufficientCapacity', async () => {
    const wrapper = mount(SessionCard, {
      props: {
        session: baseSession,
        activeRunCount: 0,
        maxConcurrentRuns: 2,
      },
    })

    const startBtn = wrapper.findAll('button').find(b => b.text() === 'Start session')
    expect(startBtn.attributes('disabled')).toBeDefined()
    expect(startBtn.attributes('title')).toMatch(/Needs 3 run slots/)

    await startBtn.trigger('click')
    expect(wrapper.emitted('start')).toBeUndefined()
  })

  it('emitsStartWhenCapacityAvailable', async () => {
    const wrapper = mount(SessionCard, {
      props: {
        session: baseSession,
        activeRunCount: 0,
        maxConcurrentRuns: 8,
      },
    })

    const startBtn = wrapper.findAll('button').find(b => b.text() === 'Start session')
    await startBtn.trigger('click')
    expect(wrapper.emitted('start')).toEqual([['session-b-alert-quality']])
  })
})
