import { apiFetch } from '@/api/client';

export function fetchDocsStats() {
  return apiFetch<unknown>('/api/docs/api/stats');
}

export function fetchDocuments(docType?: string, limit?: number, offset?: number) {
  const params = new URLSearchParams();
  if (docType) params.set('doc_type', docType);
  if (limit != null) params.set('limit', String(limit));
  if (offset != null) params.set('offset', String(offset));
  const qs = params.toString();
  return apiFetch<unknown>(`/api/docs/api/documents${qs ? `?${qs}` : ''}`);
}

export function searchDocs(query: string, mode?: string, topK?: number, docType?: string) {
  return apiFetch<unknown>('/api/docs/api/search', {
    method: 'POST',
    headers: { 'Content-Type': 'application/json' },
    body: JSON.stringify({ query, mode, top_k: topK, doc_type: docType }),
  });
}

export function fetchDocsCommunities() {
  return apiFetch<unknown>('/api/docs/api/communities');
}

export function fetchDocsEntities(entityName?: string, depth?: number, maxNodes?: number) {
  const params = new URLSearchParams();
  if (entityName) params.set('entity_name', entityName);
  if (depth != null) params.set('depth', String(depth));
  if (maxNodes != null) params.set('max_nodes', String(maxNodes));
  const qs = params.toString();
  return apiFetch<unknown>(`/api/docs/api/entities${qs ? `?${qs}` : ''}`);
}

export function fetchDocsGraphNeighborhood() {
  return apiFetch<unknown>('/api/docs/api/graph/neighborhood');
}

export function triggerFinalize() {
  return apiFetch<unknown>('/api/docs/api/finalize', { method: 'POST' });
}
