import { describe, it, expect, beforeEach } from 'vitest'
import { mount } from '@vue/test-utils'
import { createPinia, setActivePinia } from 'pinia'
import AcknowledgeModal from '@/components/alerts/AcknowledgeModal.vue'
import { useAuthStore } from '@/stores/auth'

const alert = {
  id: 'alert-1',
  alertType: 'SOFA_SEPSIS',
  severity: 'CRITICAL',
  status: 'OPEN',
  details: 'SOFA delta >= 2',
}

describe('AcknowledgeModal', () => {
  beforeEach(() => {
    setActivePinia(createPinia())
    const auth = useAuthStore()
    auth.$patch({
      user: {
        userId: '11111111-1111-1111-1111-111111111111',
        username: 'nurse.demo',
        displayName: 'Demo Nurse',
        role: 'NURSE',
      },
    })
  })

  it('showsAuthenticatedUserAndRoleContext', () => {
    const wrapper = mount(AcknowledgeModal, {
      props: { open: true, alert },
      global: {
        stubs: {
          Modal: {
            props: ['open', 'title'],
            template: '<div v-if="open"><slot /></div>',
          },
        },
      },
    })

    expect(wrapper.text()).toContain('Demo Nurse')
    expect(wrapper.text()).toContain('Nurse')
    expect(wrapper.text()).toContain('documenting awareness')
    expect(wrapper.text()).toContain('[NURSE] Acknowledged by Demo Nurse.')
  })

  it('emitsOptionalNoteOnConfirm', async () => {
    const wrapper = mount(AcknowledgeModal, {
      props: { open: true, alert },
      global: {
        stubs: {
          Modal: {
            props: ['open', 'title'],
            template: '<div v-if="open"><slot /></div>',
          },
        },
      },
    })

    await wrapper.find('textarea').setValue('Will reassess in 30 minutes')
    const ackButton = wrapper.findAll('button').find(button => button.text() === 'Acknowledge')
    await ackButton.trigger('click')

    expect(wrapper.emitted('confirm')).toEqual([['Will reassess in 30 minutes']])
  })
})
