import { apiClient } from './client';
import type { Metrics, EquityPoint, PnlDistribution } from '../types/models';

export async function getMetrics(): Promise<Metrics> {
  const response = await apiClient.get<Metrics>('/api/metrics');
  return response.data;
}

export async function getEquityCurve(): Promise<EquityPoint[]> {
  const response = await apiClient.get<EquityPoint[]>('/api/metrics/equity');
  return response.data;
}

export async function getPnlDistribution(): Promise<PnlDistribution[]> {
  const response = await apiClient.get<any>('/api/metrics/pnl-distribution');
  
  // Map backend response { bins: [], counts: [], wins: [], losses: [] } to frontend PnlDistribution[]
  if (response.data && Array.isArray(response.data.bins) && Array.isArray(response.data.counts)) {
    return response.data.bins.map((range: string, index: number) => ({
      range: range,
      count: response.data.counts[index]
    }));
  }
  
  return response.data || [];
}
