import { apiDelete, apiGet, apiPost, apiRequest } from "./client";

export type AgentProviderType = "openclaw";

export interface AgentProviderCredentials {
  apiToken?: string;
}

export interface AgentProviderConfig {
  id: string;
  workspaceId: string;
  name: string;
  provider: AgentProviderType;
  gatewayUrl: string;
  credentials: AgentProviderCredentials | null;
  isActive: boolean;
  createdAt: string;
  updatedAt: string;
}

export interface CreateAgentProviderRequest {
  name: string;
  provider: AgentProviderType;
  gatewayUrl: string;
  credentials: {
    apiToken: string;
  };
  isActive: boolean;
}

export interface UpdateAgentProviderRequest {
  name?: string;
  provider?: AgentProviderType;
  gatewayUrl?: string;
  credentials?: AgentProviderCredentials;
  isActive?: boolean;
}

export async function fetchAgentProviders(): Promise<AgentProviderConfig[]> {
  return apiGet<AgentProviderConfig[]>("/api/agent-providers");
}

export async function createAgentProvider(
  data: CreateAgentProviderRequest
): Promise<AgentProviderConfig> {
  return apiPost<AgentProviderConfig>("/api/agent-providers", data);
}

export async function updateAgentProvider(
  providerId: string,
  data: UpdateAgentProviderRequest
): Promise<AgentProviderConfig> {
  return apiRequest<AgentProviderConfig>(`/api/agent-providers/${providerId}`, {
    method: "PUT",
    body: JSON.stringify(data),
  });
}

export async function deleteAgentProvider(providerId: string): Promise<void> {
  await apiDelete<unknown>(`/api/agent-providers/${providerId}`);
}
