import { describe, it, expect, vi } from 'vitest'
import { render, screen } from '@testing-library/react'
import userEvent from '@testing-library/user-event'
import { ProcessorTimeline } from './ProcessorTimeline'

const processors = [
  { name: 'Validate', type: 'validator', durationMs: 12, status: 'ok' as const, startMs: 0 },
  { name: 'Enrich', type: 'enricher', durationMs: 35, status: 'slow' as const, startMs: 12 },
  { name: 'Route', type: 'router', durationMs: 8, status: 'fail' as const, startMs: 47 },
]

describe('ProcessorTimeline', () => {
  it('renders processor names', () => {
    render(<ProcessorTimeline processors={processors} totalMs={55} />)
    expect(screen.getByText('Validate')).toBeInTheDocument()
    expect(screen.getByText('Enrich')).toBeInTheDocument()
    expect(screen.getByText('Route')).toBeInTheDocument()
  })

  it('does not render action trigger when no actions provided', () => {
    const { container } = render(
      <ProcessorTimeline processors={processors} totalMs={55} />,
    )
    expect(container.querySelector('[aria-label*="Actions for"]')).not.toBeInTheDocument()
  })

  it('renders action trigger when actions provided', () => {
    const { container } = render(
      <ProcessorTimeline
        processors={processors}
        totalMs={55}
        actions={[{ label: 'Change Log Level', onClick: () => {} }]}
      />,
    )
    const triggers = container.querySelectorAll('[aria-label*="Actions for"]')
    expect(triggers.length).toBe(3)
  })

  it('clicking action trigger does not fire onProcessorClick', async () => {
    const onProcessorClick = vi.fn()
    const user = userEvent.setup()
    const { container } = render(
      <ProcessorTimeline
        processors={processors}
        totalMs={55}
        onProcessorClick={onProcessorClick}
        actions={[{ label: 'Test Action', onClick: () => {} }]}
      />,
    )
    const trigger = container.querySelector('[aria-label="Actions for Validate"]')!
    await user.click(trigger)
    expect(onProcessorClick).not.toHaveBeenCalled()
  })

  it('calls action onClick when menu item clicked', async () => {
    const actionClick = vi.fn()
    const user = userEvent.setup()
    const { container } = render(
      <ProcessorTimeline
        processors={processors}
        totalMs={55}
        actions={[{ label: 'Change Log Level', onClick: actionClick }]}
      />,
    )
    const trigger = container.querySelector('[aria-label="Actions for Validate"]')!
    await user.click(trigger)
    await user.click(screen.getByText('Change Log Level'))
    expect(actionClick).toHaveBeenCalledOnce()
  })

  it('supports dynamic getActions per processor', () => {
    const { container } = render(
      <ProcessorTimeline
        processors={processors}
        totalMs={55}
        getActions={(proc) =>
          proc.status === 'fail'
            ? [{ label: 'View Error', onClick: () => {} }]
            : []
        }
      />,
    )
    // Only the failing processor should have an action trigger
    const triggers = container.querySelectorAll('[aria-label*="Actions for"]')
    expect(triggers.length).toBe(1)
    expect(triggers[0]).toHaveAttribute('aria-label', 'Actions for Route')
  })
})
