import { describe, it, expect } from 'vitest'
import { mount } from '@vue/test-utils'
import ScanViewer from '@/components/ScanViewer.vue'
import WorkstationLayout from '@/components/WorkstationLayout.vue'
import WorkstationQueueRail from '@/components/WorkstationQueueRail.vue'
import type { BatchDetailResponse } from '@/types'

function makeBatch(overrides: Partial<BatchDetailResponse> = {}): BatchDetailResponse {
  return {
    id: 'batch-aaaaaaaa',
    status: 'IN_ENTRY',
    batchType: 'VITALS',
    track: 'TRACK_A',
    createdAt: '2026-01-15T10:00:00Z',
    ...overrides,
  } as BatchDetailResponse
}

describe('WorkstationLayout', () => {
  it('renders full-width queue when no batch is open', () => {
    const wrapper = mount(WorkstationLayout, {
      props: { hasBatch: false },
      slots: {
        queue: '<h2>Data Entry Queue</h2>',
        scan: '<div>scan</div>',
        form: '<div>form</div>',
      },
    })
    expect(wrapper.find('[data-testid="workstation-queue"]').exists()).toBe(true)
    expect(wrapper.find('[data-testid="workstation-split"]').exists()).toBe(false)
    expect(wrapper.text()).toContain('Data Entry Queue')
  })

  it('renders scan + form split when a batch is open', () => {
    const wrapper = mount(WorkstationLayout, {
      props: { hasBatch: true },
      slots: {
        scan: '<div data-testid="slot-scan">scan</div>',
        form: '<div data-testid="slot-form">form</div>',
      },
    })
    expect(wrapper.find('[data-testid="workstation-split"]').exists()).toBe(true)
    expect(wrapper.find('[data-testid="workstation-split"]').classes()).toContain(
      'workstation-split--no-rail'
    )
    expect(wrapper.find('[data-testid="slot-scan"]').exists()).toBe(true)
    expect(wrapper.find('[data-testid="slot-form"]').exists()).toBe(true)
    expect(wrapper.find('[data-testid="workstation-rail"]').exists()).toBe(false)
    expect(wrapper.find('[data-testid="evidence-level-1"]').text()).toContain('Source scan')
  })

  it('shows left rail when rail slot is provided', () => {
    const wrapper = mount(WorkstationLayout, {
      props: { hasBatch: true },
      slots: {
        rail: '<div>rail items</div>',
        scan: '<div>scan</div>',
        form: '<div>form</div>',
      },
    })
    expect(wrapper.find('[data-testid="workstation-rail"]').exists()).toBe(true)
    expect(wrapper.find('[data-testid="workstation-split"]').classes()).not.toContain(
      'workstation-split--no-rail'
    )
    expect(wrapper.text()).toContain('rail items')
  })
})

describe('WorkstationQueueRail', () => {
  it('lists batches and emits select / back', async () => {
    const wrapper = mount(WorkstationQueueRail, {
      props: {
        title: 'Entry queue',
        batches: [
          makeBatch({ id: 'batch-11111111' }),
          makeBatch({ id: 'batch-22222222', status: 'PENDING_VERIFICATION' }),
        ],
        selectedId: 'batch-11111111',
      },
    })
    expect(wrapper.text()).toContain('Entry queue')
    expect(wrapper.text()).toContain('batch-11')
    expect(wrapper.text()).toContain('Pending Verification')

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

    await wrapper.findAll('button').at(-1)!.trigger('click')
    expect(wrapper.emitted('back')).toHaveLength(1)
  })
})

describe('ScanViewer', () => {
  it('renders neutral toolbar with zoom and fit width', () => {
    const wrapper = mount(ScanViewer, {
      props: { url: 'blob:http://localhost/doc.png' },
    })
    const toolbar = wrapper.find('[data-testid="scan-viewer-toolbar"]')
    expect(toolbar.exists()).toBe(true)
    expect(toolbar.classes()).toContain('bg-surface')
    expect(wrapper.find('[data-testid="scan-fit-width"]').exists()).toBe(true)
    expect(wrapper.text()).toContain('Fit width')
    expect(wrapper.text()).toContain('Rotate')
  })

  it('shows skeleton while loading', () => {
    const wrapper = mount(ScanViewer, {
      props: { loading: true },
    })
    expect(wrapper.find('[role="status"]').exists()).toBe(true)
    expect(wrapper.find('img').exists()).toBe(false)
  })

  it('shows inline error with retry and preserves draft copy', async () => {
    const wrapper = mount(ScanViewer, {
      props: { error: 'Network failed', loading: false },
    })
    expect(wrapper.text()).toContain('Could not load document')
    expect(wrapper.text()).toContain('Network failed')
    expect(wrapper.text()).toContain('Your form draft was not affected.')
    const retry = wrapper.findAll('button').find((b) => b.text() === 'Retry')
    expect(retry).toBeTruthy()
    await retry!.trigger('click')
    expect(wrapper.emitted('retry')).toHaveLength(1)
  })

  it('does not put decorative AI chrome on the page surface', () => {
    const wrapper = mount(ScanViewer, {
      props: { url: 'blob:http://localhost/scan.jpg' },
    })
    expect(wrapper.text()).not.toContain('confidence')
    expect(wrapper.text()).not.toContain('OCR')
    expect(wrapper.find('.scan-page').exists()).toBe(true)
  })
})
