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

const baseScenario = {
  id: 'stable-baseline-01',
  name: 'Stable Baseline — Routine Inpatient Monitoring',
  description: '52-year-old female admitted for elective cholecystectomy. Completely uneventful 8-hour post-op monitoring period with all vitals remaining normal throughout. This is a CONTROL scenario that validates the system does NOT generate false alerts on a stable patient.',
  durationMinutes: 480,
  tags: ['stable', 'baseline', 'control', 'surgery'],
  department: 'Surgery',
  eventCount: 312,
}

describe('ScenarioCard', () => {
  it('rendersMetadata', () => {
    const wrapper = mount(ScenarioCard, {
      props: { scenario: baseScenario },
    })

    expect(wrapper.text()).toContain(baseScenario.name)
    expect(wrapper.text()).toContain('8 hours of monitoring')
    expect(wrapper.text()).toContain('Surgery')
    expect(wrapper.text()).toContain('312 events')
    expect(wrapper.text()).toContain('control')
  })

  it('labelsControlScenario', () => {
    const wrapper = mount(ScenarioCard, {
      props: { scenario: baseScenario },
    })
    expect(wrapper.text()).toContain('Control scenario — should stay quiet')
  })

  it('labelsRealDataScenario', () => {
    const wrapper = mount(ScenarioCard, {
      props: {
        scenario: {
          ...baseScenario,
          id: 'mimic-case-01',
          tags: ['mimic-iv', 'real-data', 'sepsis'],
        },
      },
    })
    expect(wrapper.text()).toContain('Real de-identified ICU record')
  })

  it('disablesStartAtConcurrencyLimit', async () => {
    const wrapper = mount(ScenarioCard, {
      props: {
        scenario: baseScenario,
        atConcurrencyLimit: true,
        maxConcurrentRuns: 2,
      },
    })

    const startBtn = wrapper.findAll('button').find(b => b.text() === 'Start')
    expect(startBtn.attributes('disabled')).toBeDefined()
    expect(startBtn.attributes('title')).toMatch(/Concurrency limit reached/)

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

  it('emitsStartWithScenarioId', async () => {
    const wrapper = mount(ScenarioCard, {
      props: { scenario: baseScenario },
    })
    const startBtn = wrapper.findAll('button').find(b => b.text() === 'Start')
    await startBtn.trigger('click')
    expect(wrapper.emitted('start')).toEqual([['stable-baseline-01']])
  })
})
