import { describe, it, expect } from 'vitest'
import { mount } from '@vue/test-utils'
import { computed, ref } from 'vue'
import WorkstationActionBar from '@/components/WorkstationActionBar.vue'
import OcrConfidenceBadge from '@/components/OcrConfidenceBadge.vue'
import {
  confidenceToLevel,
  formatOcrBadgeLabel,
  useOcrFieldConfidence,
  OCR_HIGH_THRESHOLD,
  OCR_MEDIUM_THRESHOLD,
} from '@/composables/useOcrFieldConfidence'
import type { OcrConfidenceMap } from '@/types'

describe('WorkstationActionBar', () => {
  it('renders sticky bar with left, center, primary, and right slots', () => {
    const wrapper = mount(WorkstationActionBar, {
      slots: {
        left: '<button class="btn-danger">Reject</button>',
        center: '<button class="btn-secondary">Save Draft</button>',
        primary: '<button class="btn-primary">Submit for Verification</button>',
        right: '<button class="btn-secondary">Next</button>',
      },
    })

    const bar = wrapper.find('[data-testid="workstation-action-bar"]')
    expect(bar.exists()).toBe(true)
    expect(bar.classes()).toContain('sticky')
    expect(bar.classes()).toContain('bottom-0')
    expect(wrapper.text()).toContain('Reject')
    expect(wrapper.text()).toContain('Save Draft')
    expect(wrapper.text()).toContain('Submit for Verification')
    expect(wrapper.text()).toContain('Next')
  })
})

describe('OCR confidence thresholds (design-doc §14)', () => {
  it('maps 95%+ to high, 80–94% to medium, below 80% to low', () => {
    expect(confidenceToLevel(0.95)).toBe('high')
    expect(confidenceToLevel(1)).toBe('high')
    expect(confidenceToLevel(0.94)).toBe('medium')
    expect(confidenceToLevel(OCR_MEDIUM_THRESHOLD)).toBe('medium')
    expect(confidenceToLevel(0.79)).toBe('low')
    expect(OCR_HIGH_THRESHOLD).toBe(0.95)
    expect(OCR_MEDIUM_THRESHOLD).toBe(0.8)
  })

  it('formats badge labels as OCR N%', () => {
    expect(formatOcrBadgeLabel(0.98)).toBe('OCR 98%')
    expect(formatOcrBadgeLabel(0.8)).toBe('OCR 80%')
  })

  it('exposes confidence, label, level, and border class from composable', () => {
    const map = ref<OcrConfidenceMap | null>({
      provider: 'test',
      fieldConfidences: {
        'patient.fullName': 0.98,
        'patient.sex': 0.85,
        'encounter.roomBed': 0.5,
      },
    })
    const {
      getFieldConfidence,
      fieldConfidenceLabel,
      fieldConfidenceLevel,
      fieldConfidenceClass,
    } = useOcrFieldConfidence(computed(() => map.value))

    expect(getFieldConfidence('patient.fullName')).toBe(0.98)
    expect(fieldConfidenceLabel('patient.fullName')).toBe('OCR 98%')
    expect(fieldConfidenceLevel('patient.fullName')).toBe('high')
    expect(fieldConfidenceClass('patient.fullName')).toBe('ocr-high')

    expect(fieldConfidenceLevel('patient.sex')).toBe('medium')
    expect(fieldConfidenceClass('patient.sex')).toBe('ocr-medium')

    expect(fieldConfidenceLevel('encounter.roomBed')).toBe('low')
    expect(fieldConfidenceClass('encounter.roomBed')).toBe('ocr-low')

    expect(fieldConfidenceLabel('missing.path')).toBeNull()
    expect(fieldConfidenceClass('missing.path')).toBe('')
  })
})

describe('OcrConfidenceBadge', () => {
  it('renders OCR percentage badge from confidence', () => {
    const wrapper = mount(OcrConfidenceBadge, { props: { confidence: 0.98 } })
    expect(wrapper.text()).toBe('OCR 98%')
    expect(wrapper.classes()).toContain('ocr-badge-high')
  })

  it('hides when confidence is missing', () => {
    const wrapper = mount(OcrConfidenceBadge, { props: { confidence: null } })
    expect(wrapper.find('span').exists()).toBe(false)
  })

  it('uses provided label and level', () => {
    const wrapper = mount(OcrConfidenceBadge, {
      props: { label: 'OCR 72%', level: 'low' },
    })
    expect(wrapper.text()).toBe('OCR 72%')
    expect(wrapper.classes()).toContain('ocr-badge-low')
  })
})
