import type { UploadResponse, BuildGraphResponse, CausalTriple, GraphData, ChatResponse, AppSettings, } from './types' const BASE = '/api' async function req(path: string, options?: RequestInit): Promise { const res = await fetch(BASE + path, options) if (!res.ok) { const text = await res.text() throw new Error(text || `HTTP ${res.status}`) } return res.json() as Promise } export const api = { health: () => req<{ status: string; neo4j: string }>('/health'), upload: (file: File): Promise => { const fd = new FormData() fd.append('file', file) return req('/upload', { method: 'POST', body: fd }) }, loadSample: (): Promise => req('/sample'), buildGraph: (documentId: string, settings: Partial): Promise => req('/build-graph', { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ document_id: documentId, confidence_threshold: settings.confidenceThreshold ?? 0.5, max_chunks: 50, }), }), getTriples: (documentId: string): Promise<{ triples: CausalTriple[] }> => req<{ triples: CausalTriple[] }>(`/triples?document_id=${documentId}`), getGraph: (documentId: string): Promise => req(`/graph?document_id=${documentId}`), chat: (documentId: string, message: string, settings: Partial): Promise => req('/chat', { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ document_id: documentId, message, max_depth: settings.maxDepth ?? 5, top_k_chains: settings.topKChains ?? 5, theme: settings.theme || null, }), }), reset: () => req<{ status: string }>('/reset', { method: 'POST' }), }