import { Asset, DashboardOverview, LiveNewsArticle, MarketSentiment, PipelineStatus, RelatedNews, Signal } from "./types"; const API_BASE = process.env.NEXT_PUBLIC_API_BASE ?? ""; async function getJson(path: string): Promise { const response = await fetch(`${API_BASE}${path}`, { cache: "no-store" }); if (!response.ok) { throw new Error(`${response.status} ${response.statusText}`); } return response.json() as Promise; } async function postJson(path: string, payload: unknown): Promise { const response = await fetch(`${API_BASE}${path}`, { method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify(payload) }); if (!response.ok) { throw new Error(`${response.status} ${response.statusText}`); } return response.json() as Promise; } export const api = { assets: () => getJson("/assets"), asset: (ticker: string) => getJson<{ asset: Asset; market_snapshot: Asset["market_snapshot"]; prices: any[]; latest_signal: Signal | null; related_news: RelatedNews[] }>(`/assets/${ticker}`), overview: () => getJson("/dashboard/overview"), liveNews: (limit = 60) => getJson(`/news/live?limit=${limit}`), marketSentiment: (hours = 48) => getJson(`/sentiment/market?hours=${hours}`), pipelineStatus: () => getJson("/pipeline/status"), topSignals: (query = "") => getJson(`/signals/top${query}`), signal: (ticker: string) => getJson(`/signals/${ticker}`), sentiment: (ticker: string) => getJson(`/sentiment/${ticker}`), explain: (ticker: string) => getJson(`/ai/explain/${ticker}`), relatedNews: (ticker: string) => getJson(`/related-news?ticker=${ticker}`), themes: () => getJson("/themes"), etfTrends: () => getJson("/etf-trends"), semanticSearch: (query: string) => postJson("/semantic-search", { query, limit: 12 }), marketUpdate: () => postJson("/market/update", { period: "max", limit: 36 }), newsUpdate: () => postJson("/news/update", { lookback_hours: 72, limit_per_feed: 35 }), runSignals: () => postJson("/signals/run", { refresh_prices: false, limit: 36 }), runPipeline: () => postJson("/pipeline/run", { refresh_prices: false, limit: 36 }), backtest: (ticker: string) => postJson(`/backtest/${ticker}`, {}) };