import { describe, it, expect } from 'vitest'
import { mount } from '@vue/test-utils'
import WorkstationQueueRail from '@/components/WorkstationQueueRail.vue'
import type { BatchDetailResponse } from '@/types'
import { fieldRequirementsForBatchType } from '@/__tests__/helpers/fieldRequirements'

function makeBatch(overrides: Partial<BatchDetailResponse> = {}): BatchDetailResponse {
  const batchType = overrides.batchType ?? 'VITALS'
  return {
    id: 'abcdef12-3456-7890-abcd-ef1234567890',
    status: 'PENDING_ENTRY',
    batchType,
    track: 'TRACK_A',
    fieldRequirements: fieldRequirementsForBatchType(batchType),
    patientId: 'p1',
    documentRef: 'docs/scan.pdf',
    documentUrl: null,
    enableRetroactiveAlerts: false,
    enteredByUserId: null,
    verifiedByUserId: null,
    approvedByUserId: null,
    rejectionReason: null,
    promotedAt: null,
    promotionEncounterId: null,
    supersedesBatchId: null,
    clinicianAttestation: false,
    isCorrection: false,
    supersession: null,
    createdAt: '2026-06-27T10:00:00Z',
    updatedAt: '2026-06-27T10:00:00Z',
    ...overrides,
  }
}

describe('WorkstationQueueRail', () => {
  it('shows batch type as title with status badge and truncated id as meta', () => {
    const wrapper = mount(WorkstationQueueRail, {
      props: {
        batches: [makeBatch({ batchType: 'ADMISSION', status: 'PENDING_ENTRY' })],
        selectedId: 'abcdef12-3456-7890-abcd-ef1234567890',
      },
    })

    const item = wrapper.find('button[aria-current="true"]')
    expect(item.exists()).toBe(true)
    const text = item.text()
    expect(text).toContain('Admission')
    expect(text).toContain('Pending Entry')
    expect(text).toContain('abcdef12…')

    const html = item.html()
    const typeIdx = html.indexOf('Admission')
    const badgeIdx = html.indexOf('Pending Entry')
    const idIdx = html.indexOf('abcdef12…')
    expect(typeIdx).toBeGreaterThan(-1)
    expect(badgeIdx).toBeGreaterThan(typeIdx)
    expect(idIdx).toBeGreaterThan(badgeIdx)
  })

  it('emits select when a queue item is clicked', async () => {
    const batch = makeBatch()
    const wrapper = mount(WorkstationQueueRail, {
      props: { batches: [batch] },
    })

    await wrapper.findAll('button')[0].trigger('click')
    expect(wrapper.emitted('select')?.[0]).toEqual([batch.id])
  })

  it('emits back from Full queue action', async () => {
    const wrapper = mount(WorkstationQueueRail, {
      props: { batches: [] },
    })

    await wrapper.get('button').trigger('click')
    expect(wrapper.emitted('back')).toBeTruthy()
  })
})
