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

describe('SeparationOfDutiesBanner', () => {
  it('shows enforced info when entered-by and verifier differ', () => {
    const wrapper = mount(SeparationOfDutiesBanner, {
      props: {
        enteredByUserId: 'entry-user-1',
        enteredByUserName: 'Arjun Menon',
        currentUserId: 'verify-user-2',
        currentUserName: 'Priya Nair',
      },
    })

    expect(wrapper.text()).toContain('Separation of Duties Enforced')
    expect(wrapper.text()).toContain('Arjun Menon')
    expect(wrapper.text()).toContain('Priya Nair')
    expect(wrapper.text()).not.toContain('You cannot verify')
    expect(wrapper.emitted('update:blocked')?.at(-1)).toEqual([false])
  })

  it('blocks when the same user entered the batch', () => {
    const wrapper = mount(SeparationOfDutiesBanner, {
      props: {
        enteredByUserId: 'same-user',
        currentUserId: 'same-user',
        currentUserName: 'Alex Clerk',
      },
    })

    expect(wrapper.text()).toContain('You cannot verify a batch you entered.')
    expect(wrapper.emitted('update:blocked')?.at(-1)).toEqual([true])
  })

  it('falls back to truncated ids when names are omitted', () => {
    const wrapper = mount(SeparationOfDutiesBanner, {
      props: {
        enteredByUserId: 'abcdefghijkl',
        currentUserId: 'mnopqrstuvwx',
      },
    })

    expect(wrapper.text()).toContain('abcdefgh…')
    expect(wrapper.text()).toContain('mnopqrst…')
  })

  it('hides when either id is missing', () => {
    const wrapper = mount(SeparationOfDutiesBanner, {
      props: {
        enteredByUserId: 'entry-1',
        currentUserId: '',
      },
    })
    expect(wrapper.find('[data-testid="sod-banner"]').exists()).toBe(false)
  })
})

describe('ConfirmDialog', () => {
  const teleportStub = {
    global: {
      stubs: {
        Teleport: { template: '<div><slot /></div>' },
      },
    },
  }

  it('renders title, body, and confirm label when open', () => {
    const wrapper = mount(ConfirmDialog, {
      props: {
        open: true,
        title: 'Approve & Promote',
        body: 'Approval will promote the verified records into live clinical tables.',
        confirmLabel: 'Approve & Promote',
        variant: 'primary',
      },
      ...teleportStub,
    })

    expect(wrapper.text()).toContain('Approve & Promote')
    expect(wrapper.text()).toContain(
      'Approval will promote the verified records into live clinical tables.'
    )
  })

  it('emits confirm and cancel', async () => {
    const wrapper = mount(ConfirmDialog, {
      props: {
        open: true,
        title: 'Reject Batch',
        confirmLabel: 'Confirm Rejection',
        variant: 'danger',
      },
      ...teleportStub,
    })

    const buttons = wrapper.findAll('button')
    const confirmBtn = buttons.find((b) => b.text() === 'Confirm Rejection')
    const cancelBtn = buttons.find((b) => b.text() === 'Cancel')
    await confirmBtn!.trigger('click')
    await cancelBtn!.trigger('click')
    expect(wrapper.emitted('confirm')).toHaveLength(1)
    expect(wrapper.emitted('cancel')).toHaveLength(1)
  })

  it('disables confirm when confirmDisabled is true', () => {
    const wrapper = mount(ConfirmDialog, {
      props: {
        open: true,
        title: 'Reject Batch',
        confirmLabel: 'Confirm Rejection',
        variant: 'danger',
        confirmDisabled: true,
      },
      ...teleportStub,
    })

    const confirmBtn = wrapper
      .findAll('button')
      .find((b) => b.text() === 'Confirm Rejection')
    expect(confirmBtn!.attributes('disabled')).toBeDefined()
  })
})
