import { apiClient } from './client';
import type { AgentStatus } from '../types/models';

export async function getAgents(): Promise<AgentStatus[]> {
  const response = await apiClient.get<any[]>('/api/agents');
  
  // Map backend response fields to frontend AgentStatus interface
  return response.data.map(agent => ({
    id: agent.agent_id || agent.id || String(Math.random()),
    name: agent.name || agent.agent_id || 'Unknown',
    status: agent.status ? (agent.status.includes('stable') || agent.status.includes('thriving') ? 'running' : 
           agent.status.includes('bankrupt') ? 'stopped' : 
           agent.status.includes('critical') ? 'error' : 'paused') : 'paused',
    strategy: agent.strategy || agent.current_activity || 'Unknown',
    symbol: agent.symbol || 'N/A',
    profitLoss: agent.balance - agent.initial_capital,
    profitLossPercent: ((agent.balance - agent.initial_capital) / agent.initial_capital) * 100,
    tradesCount: agent.total_trades || 0,
    lastUpdated: agent.last_updated || new Date().toISOString()
  }));
}

export async function getAgent(id: string): Promise<AgentStatus> {
  const response = await apiClient.get<AgentStatus>(`/api/agents/${id}`);
  return response.data;
}
