import { describe, it, expect, vi } from 'vitest'
import { render, screen, fireEvent } from '@testing-library/react'
import { ChunkList } from '../../components/ChunkList'
import type { ChunkInfo } from '../../types'

vi.mock('../../lib/api', () => ({
  getChunkPdfUrl: (path: string) => `http://localhost:8000/api/v1/chunks/${path}/pdf`,
}))

const mockChunks: ChunkInfo[] = [
  {
    chunk_id: 'doc1_0',
    chunk_index: 0,
    content_summary: 'Summary of chunk 0 content...',
    page_number: 1,
    chunk_file_path: 'test_page_1.pdf',
  },
  {
    chunk_id: 'doc1_1',
    chunk_index: 1,
    content_summary: 'Summary of chunk 1 content that is quite long and should be truncated when displayed in the component',
    page_number: 2,
    chunk_file_path: 'test_page_2.pdf',
  },
  {
    chunk_id: 'doc1_2',
    chunk_index: 2,
    content_summary: 'A DOCX chunk without page info',
    page_number: null,
    chunk_file_path: null,
  },
]

describe('ChunkList', () => {
  it('renders chunk rows with chunk index and page number', () => {
    render(
      <ChunkList
        chunks={mockChunks}
        isLoading={false}
        onDeleteChunk={vi.fn()}
        isDeleting={false}
      />
    )

    expect(screen.getByText(/Chunk 0/)).toBeInTheDocument()
    expect(screen.getByText(/Chunk 1/)).toBeInTheDocument()
    expect(screen.getByText(/Chunk 2/)).toBeInTheDocument()
    expect(screen.getByText(/Page: 1/)).toBeInTheDocument()
    expect(screen.getByText(/Page: 2/)).toBeInTheDocument()
    expect(screen.getByText(/Page: N\/A/)).toBeInTheDocument()
  })

  it('renders View PDF links for chunks with chunk_file_path', () => {
    render(
      <ChunkList
        chunks={mockChunks}
        isLoading={false}
        onDeleteChunk={vi.fn()}
        isDeleting={false}
      />
    )

    const links = screen.getAllByTestId('view-chunk-pdf-link')
    expect(links).toHaveLength(2)
    expect(links[0]).toHaveAttribute('href', 'http://localhost:8000/api/v1/chunks/test_page_1.pdf/pdf')
    expect(links[0]).toHaveAttribute('target', '_blank')
  })

  it('does not render View PDF link for chunks without chunk_file_path', () => {
    const docxChunk = [mockChunks[2]]
    render(
      <ChunkList
        chunks={docxChunk}
        isLoading={false}
        onDeleteChunk={vi.fn()}
        isDeleting={false}
      />
    )

    expect(screen.queryByTestId('view-chunk-pdf-link')).not.toBeInTheDocument()
  })

  it('calls onDeleteChunk when delete button is clicked', () => {
    const onDelete = vi.fn()
    render(
      <ChunkList
        chunks={[mockChunks[0]]}
        isLoading={false}
        onDeleteChunk={onDelete}
        isDeleting={false}
      />
    )

    fireEvent.click(screen.getByTestId('delete-chunk-btn'))
    expect(onDelete).toHaveBeenCalledWith('doc1_0')
  })

  it('shows loading skeletons when isLoading is true', () => {
    render(
      <ChunkList
        chunks={[]}
        isLoading={true}
        onDeleteChunk={vi.fn()}
        isDeleting={false}
      />
    )

    expect(screen.getAllByTestId('skeleton-line').length).toBeGreaterThan(0)
  })

  it('shows empty message when no chunks', () => {
    render(
      <ChunkList
        chunks={[]}
        isLoading={false}
        onDeleteChunk={vi.fn()}
        isDeleting={false}
      />
    )

    expect(screen.getByText('No chunks found')).toBeInTheDocument()
  })

  it('disables delete button when isDeleting is true', () => {
    render(
      <ChunkList
        chunks={[mockChunks[0]]}
        isLoading={false}
        onDeleteChunk={vi.fn()}
        isDeleting={true}
      />
    )

    expect(screen.getByTestId('delete-chunk-btn')).toBeDisabled()
  })
})
