import { render, screen, fireEvent } from '@testing-library/react'
import { describe, it, expect, vi } from 'vitest'
import BulkActionBar from './BulkActionBar.jsx'

const FIELDS = {
  priority: { type: 'enum', values: ['P0', 'P1', 'P2'], label: 'Priority' },
  tags: { type: 'tags', label: 'Tags' },
}

describe('BulkActionBar', () => {
  it('shows the selected count', () => {
    render(<BulkActionBar fields={FIELDS} count={3} onApply={() => {}} onClear={() => {}} />)
    expect(screen.getByText(/3 selected/i)).toBeInTheDocument()
  })

  it('applies a set op when an enum value is chosen', () => {
    const onApply = vi.fn()
    render(<BulkActionBar fields={FIELDS} count={2} onApply={onApply} onClear={() => {}} />)
    fireEvent.change(screen.getByLabelText(/set priority/i), { target: { value: 'P0' } })
    expect(onApply).toHaveBeenCalledWith({ op: 'set', field: 'priority', value: 'P0' })
  })

  it('applies an add-tag op', () => {
    const onApply = vi.fn()
    render(<BulkActionBar fields={FIELDS} count={2} onApply={onApply} onClear={() => {}} />)
    fireEvent.change(screen.getByPlaceholderText(/tag…/i), { target: { value: 'checkout' } })
    fireEvent.click(screen.getByRole('button', { name: /add tag/i }))
    expect(onApply).toHaveBeenCalledWith({ op: 'add', field: 'tags', value: 'checkout' })
  })

  it('applies a remove-tag op', () => {
    const onApply = vi.fn()
    render(<BulkActionBar fields={FIELDS} count={2} onApply={onApply} onClear={() => {}} />)
    fireEvent.change(screen.getByPlaceholderText(/tag…/i), { target: { value: 'checkout' } })
    fireEvent.click(screen.getByRole('button', { name: /remove tag/i }))
    expect(onApply).toHaveBeenCalledWith({ op: 'remove', field: 'tags', value: 'checkout' })
  })

  it('calls onClear', () => {
    const onClear = vi.fn()
    render(<BulkActionBar fields={FIELDS} count={2} onApply={() => {}} onClear={onClear} />)
    fireEvent.click(screen.getByRole('button', { name: /clear/i }))
    expect(onClear).toHaveBeenCalled()
  })
})
