import React from 'react'
import { render, screen, fireEvent } from '@testing-library/react'
import { IngestPanel } from '../../components/IngestPanel'

describe('IngestPanel', () => {
  const mockOnUpload = vi.fn()

  beforeEach(() => {
    mockOnUpload.mockClear()
  })

  it('renders upload button', () => {
    render(<IngestPanel onUpload={mockOnUpload} isLoading={false} success={null} error={null} />)
    expect(screen.getByText(/upload document/i)).toBeInTheDocument()
  })

  it('button is disabled when isLoading is true', () => {
    render(<IngestPanel onUpload={mockOnUpload} isLoading={true} success={null} error={null} />)
    expect(screen.getByText(/uploading\.\.\./i).closest('label')).toHaveClass('cursor-not-allowed')
  })

  it('shows uploading state when isLoading', () => {
    render(<IngestPanel onUpload={mockOnUpload} isLoading={true} success={null} error={null} />)
    expect(screen.getByText(/uploading\.\.\./i)).toBeInTheDocument()
  })

  it('shows success message when success prop is set', () => {
    render(
      <IngestPanel onUpload={mockOnUpload} isLoading={false} success="test.pdf" error={null} />
    )
    expect(screen.getByText(/uploaded test\.pdf/i)).toBeInTheDocument()
  })

  it('shows error message when error prop is set', () => {
    render(
      <IngestPanel onUpload={mockOnUpload} isLoading={false} success={null} error="Upload failed" />
    )
    expect(screen.getByText(/upload failed/i)).toBeInTheDocument()
  })

  it('calls onUpload when file is selected', () => {
    render(<IngestPanel onUpload={mockOnUpload} isLoading={false} success={null} error={null} />)
    const fileInput = screen.getByTestId('file-input')
    const file = new File(['test content'], 'test.pdf', { type: 'application/pdf' })

    fireEvent.change(fileInput, { target: { files: [file] } })

    expect(mockOnUpload).toHaveBeenCalledTimes(1)
    expect(mockOnUpload).toHaveBeenCalledWith(file)
  })
})
