import { describe, it, expect } from 'vitest'
import { mount } from '@vue/test-utils'
import EmptyState from '@/components/EmptyState.vue'
import SkeletonBlock from '@/components/SkeletonBlock.vue'
import InlineError from '@/components/InlineError.vue'
import BatchList from '@/components/BatchList.vue'
import type { BatchDetailResponse } from '@/types'

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

describe('EmptyState', () => {
  it('renders operational title and description', () => {
    const wrapper = mount(EmptyState, {
      props: {
        title: 'No batches are waiting for verification.',
        description: 'New batches appear here after data entry is submitted.',
      },
    })
    expect(wrapper.text()).toContain('No batches are waiting for verification.')
    expect(wrapper.text()).toContain('New batches appear here after data entry is submitted.')
  })

  it('renders action slot', () => {
    const wrapper = mount(EmptyState, {
      props: { title: 'Empty' },
      slots: { action: '<button>Return to Dashboard</button>' },
    })
    expect(wrapper.find('button').text()).toBe('Return to Dashboard')
  })
})

describe('SkeletonBlock', () => {
  it('exposes loading status for table variant', () => {
    const wrapper = mount(SkeletonBlock, { props: { variant: 'table', rows: 3 } })
    expect(wrapper.attributes('role')).toBe('status')
    expect(wrapper.attributes('aria-busy')).toBe('true')
  })
})

describe('InlineError', () => {
  it('shows what happened, what was preserved, and retry', async () => {
    const wrapper = mount(InlineError, {
      props: {
        title: 'Could not load batches',
        message: 'Network error',
        preserved: 'Your filters were preserved.',
        retryLabel: 'Retry',
      },
    })
    expect(wrapper.text()).toContain('Could not load batches')
    expect(wrapper.text()).toContain('Network error')
    expect(wrapper.text()).toContain('Your filters were preserved.')
    await wrapper.find('button').trigger('click')
    expect(wrapper.emitted('retry')).toHaveLength(1)
  })
})

describe('BatchList patterns', () => {
  it('shows skeleton while loading', () => {
    const wrapper = mount(BatchList, {
      props: { batches: [], loading: true },
    })
    expect(wrapper.find('[role="status"]').exists()).toBe(true)
    expect(wrapper.text()).not.toContain('No batches')
  })

  it('shows empty state when idle with no batches', () => {
    const wrapper = mount(BatchList, {
      props: {
        batches: [],
        loading: false,
        emptyTitle: 'No batches are waiting for verification.',
        emptyDescription: 'New batches appear here after data entry is submitted.',
      },
    })
    expect(wrapper.text()).toContain('No batches are waiting for verification.')
  })

  it('shows inline error with retry over empty/loading', async () => {
    const wrapper = mount(BatchList, {
      props: {
        batches: [],
        loading: false,
        error: 'Timed out',
      },
    })
    expect(wrapper.text()).toContain('Could not load batches')
    expect(wrapper.text()).toContain('Timed out')
    await wrapper.find('button').trigger('click')
    expect(wrapper.emitted('retry')).toHaveLength(1)
  })

  it('renders StatusBadge for each batch status', () => {
    const wrapper = mount(BatchList, {
      props: {
        batches: [
          makeBatch({ id: 'b1', status: 'PENDING_VERIFICATION' }),
          makeBatch({ id: 'b2', status: 'PROMOTED' }),
        ],
        loading: false,
      },
    })
    expect(wrapper.text()).toContain('Pending Verification')
    expect(wrapper.text()).toContain('Promoted')
  })
})
