import { apiRequest } from '../../lib/api'
import type { Chunk, Document, DocumentStatus, Page } from '../../lib/types'

export function listDocuments(status?: DocumentStatus) {
  const params = new URLSearchParams({ limit: '100' })
  if (status) params.set('status', status)
  return apiRequest<Page<Document>>(`/api/v1/documents?${params}`)
}

export function getDocument(documentId: string) {
  return apiRequest<Document>(`/api/v1/documents/${documentId}`)
}

export function listChunks(documentId: string, offset = 0, limit = 20) {
  return apiRequest<Page<Chunk>>(
    `/api/v1/documents/${documentId}/chunks?offset=${offset}&limit=${limit}`,
  )
}

export function uploadDocument(file: File) {
  const form = new FormData()
  form.append('file', file)
  return apiRequest<Document>('/api/v1/documents', { method: 'POST', body: form })
}

export function retryDocument(documentId: string) {
  return apiRequest<Document>(`/api/v1/documents/${documentId}/retry`, { method: 'POST' })
}

export function deleteDocument(documentId: string) {
  return apiRequest<void>(`/api/v1/documents/${documentId}`, { method: 'DELETE' })
}
