import { api } from './http';

export interface StatsOverview {
  agentCount: number;
  sessionCount: number;
  messageCount: number;
  promptTokens?: number;
  completionTokens?: number;
  totalTokens?: number;
  costUSD?: number;
  byModel?: AgentTokenStatsByModel[];
  daily: { day: string; total: number; user: number; assistant: number }[];
  topAgents: { id: string; name: string; messageCount: number }[];
}

export interface AgentStats {
  sessionCount: number;
  messageCount: number;
  avgMessageLen: number;
  assistantWithToolCalls: number;
  knowledgeFiles: number;
  skillCount: number;
  daily: { day: string; count: number }[];
}

export interface AgentTokenStatsByModel {
  providerKind: string;
  model: string;
  calls: number;
  promptTokens: number;
  completionTokens: number;
  totalTokens: number;
  costUSD: number;
}

export interface AgentTokenStatsDaily {
  day: string;
  calls: number;
  promptTokens: number;
  completionTokens: number;
  totalTokens: number;
  costUSD: number;
}

export interface AgentTokenStats {
  days?: number;
  limit?: number;
  calls?: number;
  promptTokens?: number;
  completionTokens?: number;
  totalTokens?: number;
  costUSD?: number;
  byModel: AgentTokenStatsByModel[];
  daily: AgentTokenStatsDaily[];
}

export const StatsAPI = {
  overview: () => api.get<StatsOverview>('/stats/overview').then((r) => r.data),
  agent: (id: string) => api.get<AgentStats>(`/stats/agents/${id}`).then((r) => r.data),
  agentTokens: (id: string, opts: { days?: number; limit?: number } = {}) =>
    api.get<AgentTokenStats>(`/stats/agents/${id}/tokens`, { params: { days: opts.days ?? 30, limit: opts.limit ?? 10 } }).then((r) => r.data)
};
