import React from 'react'
import { Trash2 } from 'lucide-react'
import type { ChunkInfo } from '../types'
import { getPdfViewerUrl } from '../lib/api'

interface ChunkListProps {
  chunks: ChunkInfo[]
  isLoading: boolean
  onDeleteChunk: (chunkId: string) => void
  isDeleting: boolean
}

export const ChunkList: React.FC<ChunkListProps> = ({
  chunks,
  isLoading,
  onDeleteChunk,
  isDeleting,
}) => {
  if (isLoading) {
    return (
      <div className="space-y-2 p-4">
        <div
          data-testid="skeleton-line"
          className="h-4 bg-gray-200 rounded animate-pulse w-full"
        />
        <div
          data-testid="skeleton-line"
          className="h-4 bg-gray-200 rounded animate-pulse w-11/12"
        />
        <div
          data-testid="skeleton-line"
          className="h-4 bg-gray-200 rounded animate-pulse w-4/5"
        />
      </div>
    )
  }

  if (chunks.length === 0) {
    return (
      <div className="p-4 text-center text-gray-500">
        No chunks found
      </div>
    )
  }

  return (
    <div className="space-y-2 p-4">
      {chunks.map((chunk) => (
        <div
          key={chunk.chunk_id}
          data-testid="chunk-row"
          className="border border-gray-200 rounded p-3 bg-gray-50 flex items-start justify-between"
        >
          <div className="flex-1 min-w-0">
            <div className="flex items-center space-x-2 mb-1">
              <span className="text-xs font-medium text-gray-500 uppercase">
                Chunk {chunk.chunk_index}
              </span>
              <span className="text-xs text-gray-400">
                Page: {chunk.page_number !== null ? chunk.page_number : 'N/A'}
              </span>
            </div>
            <div className="text-sm text-gray-700 truncate" title={chunk.content_summary}>
              {chunk.content_summary.length > 100
                ? `${chunk.content_summary.slice(0, 100)}...`
                : chunk.content_summary}
            </div>
            {chunk.chunk_file_path && (
              <a
                href={getPdfViewerUrl(chunk.chunk_file_path, chunk.page_number ?? undefined)}
                target="_blank"
                rel="noopener noreferrer"
                className="inline-flex items-center mt-1 text-xs text-blue-600 hover:text-blue-800 hover:underline"
                data-testid="view-chunk-pdf-link"
              >
                View PDF
              </a>
            )}
          </div>
          <button
            data-testid="delete-chunk-btn"
            onClick={() => onDeleteChunk(chunk.chunk_id)}
            disabled={isDeleting}
            className="flex items-center space-x-1 px-2 py-1 text-sm text-red-600 hover:text-red-700 hover:bg-red-50 rounded transition-colors duration-200 disabled:opacity-50 disabled:cursor-not-allowed ml-2"
          >
            <Trash2 className="w-4 h-4" />
          </button>
        </div>
      ))}
    </div>
  )
}
