import { useQuery } from '@tanstack/react-query'
import { documentsApi } from '../api/endpoints'
import type { DocumentDetailResponse } from '../api/types'

export const useDocumentDetail = (documentId: string | null) => {
  const { data, isLoading, error, refetch } = useQuery<DocumentDetailResponse>({
    queryKey: ['document', documentId],
    queryFn: () => {
      if (!documentId) {
        throw new Error('Document ID is required')
      }
      return documentsApi.getDetail(documentId)
    },
    enabled: !!documentId,
    staleTime: 10000,
  })

  return {
    document: data || null,
    annotations: data?.annotations || [],
    isLoading,
    error,
    refetch,
  }
}
