// Admin API — /admin/models/*, /admin/sessions/*, /admin/clients/*

import { request, type ApiResponse } from './client';
import type {
  ModelProvider,
  ModelConfig,
  RoutingRule,
  SessionState,
  ClientInfo,
  HealthCheckResult,
  ModelListResponse,
  ProviderListResponse,
  AdminSessionsResponse,
  ActiveSessionsResponse,
  ClientListResponse,
} from '@/types/admin';

// ========== Providers ==========

export function listProviders(): Promise<ApiResponse<ProviderListResponse>> {
  return request<ProviderListResponse>('/admin/models/providers');
}

export function saveProvider(name: string, baseUrl: string, apiKey: string): Promise<ApiResponse<{ status: string; name: string }>> {
  return request(`/admin/models/providers/${encodeURIComponent(name)}`, {
    method: 'POST',
    body: { name, base_url: baseUrl, api_key: apiKey },
  });
}

export function deleteProvider(name: string): Promise<ApiResponse<{ status: string; name: string }>> {
  return request(`/admin/models/providers/${encodeURIComponent(name)}`, { method: 'DELETE' });
}

export function healthCheckProvider(provider: string): Promise<ApiResponse<HealthCheckResult>> {
  return request('/admin/models/health-check', { method: 'POST', body: { provider } });
}

export function fetchProviderModels(name: string, url?: string): Promise<ApiResponse<{ models: Array<{ id: string; name?: string }> }>> {
  const query = url ? `?url=${encodeURIComponent(url)}` : '';
  return request(`/admin/models/fetch-models/${encodeURIComponent(name)}${query}`);
}

// ========== Models ==========

export function listModels(): Promise<ApiResponse<ModelListResponse>> {
  return request<ModelListResponse>('/admin/models/models');
}

export function saveModel(id: string, config: Partial<ModelConfig>): Promise<ApiResponse<{ status: string; id: string }>> {
  return request(`/admin/models/models/${encodeURIComponent(id)}`, {
    method: 'POST',
    body: { id, ...config },
  });
}

export function deleteModel(id: string): Promise<ApiResponse<{ status: string; id: string }>> {
  return request(`/admin/models/models/${encodeURIComponent(id)}`, { method: 'DELETE' });
}

// ========== Routing ==========

export function listRouting(): Promise<ApiResponse<{ rules: RoutingRule[] }>> {
  return request('/admin/models/routing');
}

export function saveRouting(purpose: string, fallbackChain: string[], required?: boolean): Promise<ApiResponse<{ status: string; purpose: string }>> {
  return request(`/admin/models/routing/${encodeURIComponent(purpose)}`, {
    method: 'POST',
    body: { purpose, fallback_chain: fallbackChain, required },
  });
}

export function deleteRouting(purpose: string): Promise<ApiResponse<{ status: string; purpose: string }>> {
  return request(`/admin/models/routing/${encodeURIComponent(purpose)}`, { method: 'DELETE' });
}

// ========== Sessions ==========

export function listAdminSessions(): Promise<ApiResponse<AdminSessionsResponse>> {
  return request<AdminSessionsResponse>('/admin/sessions');
}

export function listActiveSessions(): Promise<ApiResponse<ActiveSessionsResponse>> {
  return request<ActiveSessionsResponse>('/admin/sessions/active');
}

export function getAdminSession(id: string): Promise<ApiResponse<SessionState>> {
  return request<SessionState>(`/admin/sessions/${encodeURIComponent(id)}`);
}

// ========== Clients ==========

export function listClients(userId?: string): Promise<ApiResponse<ClientListResponse>> {
  const query = userId ? `?user_id=${encodeURIComponent(userId)}` : '';
  return request<ClientListResponse>(`/admin/clients${query}`);
}

export function setClientNote(clientId: string, note: string): Promise<ApiResponse<{ status: string; client_id: string; note: string }>> {
  return request(`/admin/clients/${encodeURIComponent(clientId)}/note`, {
    method: 'PUT',
    body: { note },
  });
}
