/**
 * Admin API - Models Management
 */

import { api as client } from '../apiClient';

export interface ModelInfo {
  id: string;
  name: string;
  provider: string;
  enabled: boolean;
  isDefault: boolean;
  capabilities?: string[];
}

export interface ProviderInfo {
  id: string;
  name: string;
  enabled: boolean;
  models?: ModelInfo[];
}

export interface ModelTestResult {
  success: boolean;
  message: string;
  status?: string;
  latency?: number;
  latencyMs?: number;
}

export const listModels = async (): Promise<ModelInfo[]> => {
  const response = await client.get('/api/admin/models');
  return response.data;
};

export const toggleModel = async (id: string, _enabled?: boolean): Promise<ModelInfo> => {
  const response = await client.post(`/api/admin/models/${id}/toggle`);
  return response.data;
};

export const setDefaultModel = async (id: string): Promise<ModelInfo> => {
  const response = await client.post(`/api/admin/models/${id}/set-default`);
  return response.data;
};

export const testModel = async (id: string): Promise<ModelTestResult> => {
  const response = await client.post(`/api/admin/models/${id}/test`);
  return response.data;
};

export interface ProvidersResponse {
  providers: ProviderInfo[];
}

export const listProviders = async (): Promise<ProvidersResponse> => {
  const response = await client.get('/config/providers');
  return response.data.data;
};

export const toggleProvider = async (id: string, _enabled?: boolean): Promise<ProviderInfo> => {
  const response = await client.post(`/api/admin/providers/${id}/toggle`);
  return response.data;
};

export const deleteProvider = async (id: string): Promise<void> => {
  await client.delete(`/api/admin/providers/${id}`);
};
