import apiClient from './client';

export interface IngestionSourceResult {
  status: 'ok' | 'error' | 'skipped' | 'warning';
  message?: string | null;
  records?: number;
  classification?: string;
  confidence?: number;
}

export interface FetchDataResult {
  symbol: string;
  sources: Record<string, IngestionSourceResult>;
}

/** Provider sources that cost an API call/quota. */
export type FetchSource = 'ohlcv' | 'sentiment' | 'fundamentals';
/** Source selector: omit → fetch all; array → those providers; 'recompute' → derived only (free). */
export type FetchSelector = FetchSource[] | 'recompute';

export function fetchData(symbol: string, sources?: FetchSelector) {
  let sourcesParam: string | undefined;
  if (sources === 'recompute') sourcesParam = 'recompute';
  else if (sources && sources.length) sourcesParam = sources.join(',');
  const params = sourcesParam ? { sources: sourcesParam } : undefined;
  return apiClient
    .post<FetchDataResult>(`ingestion/fetch/${symbol}`, null, { params })
    .then((r) => r.data);
}
