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

const summary = {
  simulatedPatients: 12,
  encounters: 12,
  observations: 340,
  alerts: 28,
  activeRuns: 0,
}

describe('ResetWardPanel', () => {
  it('displaysCounts', () => {
    const wrapper = mount(ResetWardPanel, { props: { summary } })
    expect(wrapper.text()).toContain('12 simulated patients')
    expect(wrapper.text()).toContain('340 observations')
    expect(wrapper.text()).toContain('28 alerts')
  })

  it('requiresTypedConfirmation', async () => {
    const wrapper = mount(ResetWardPanel, {
      props: { summary },
      attachTo: document.body,
    })

    await wrapper.findAll('button').find(b => b.text() === 'Reset ward').trigger('click')
    await wrapper.vm.$nextTick()

    expect(document.body.textContent).toContain('Type')
    expect(document.body.textContent).toContain('RESET')

    const input = document.body.querySelector('input')
    expect(input).toBeTruthy()
    input.value = 'RESET'
    input.dispatchEvent(new Event('input'))
    await wrapper.vm.$nextTick()

    // Confirm button is the danger button inside the modal (last Reset ward).
    const buttons = [...document.body.querySelectorAll('button')]
      .filter(b => b.textContent.trim() === 'Reset ward')
    const confirmBtn = buttons.at(-1)
    expect(confirmBtn.disabled).toBe(false)

    confirmBtn.click()
    await wrapper.vm.$nextTick()
    expect(wrapper.emitted('purge')).toHaveLength(1)

    wrapper.unmount()
  })

  it('blocksWhileRunsActive', () => {
    const wrapper = mount(ResetWardPanel, {
      props: {
        summary: { ...summary, activeRuns: 2 },
      },
    })

    expect(wrapper.text()).toContain('Reset is blocked')
    expect(wrapper.text()).toContain('Stop all runs')

    const resetBtn = wrapper.findAll('button').find(b => b.text() === 'Reset ward')
    expect(resetBtn.attributes('disabled')).toBeDefined()
  })
})
