import { fetchWithAuth } from "./client";

// ── 类型定义 ────────────────────────────────────────────────────────────────────

export interface Agent {
  id: string;
  name: string;
  type: string;
  status: "idle" | "running" | "error" | "disabled";
  description?: string;
  config?: Record<string, unknown>;
  created_at: string;
  updated_at: string;
}

export interface AgentRunLog {
  id: string;
  agent_id: string;
  status: "success" | "failed" | "running";
  started_at: string;
  finished_at?: string;
  message?: string;
}

// ── API ────────────────────────────────────────────────────────────────────────

export const agentsApi = {
  list: (token: string) =>
    fetchWithAuth("/api/v1/agents/", {}, token) as Promise<Agent[]>,
  get: (token: string, agentId: string) =>
    fetchWithAuth(`/api/v1/agents/${agentId}`, {}, token) as Promise<Agent>,
  updateConfig: (token: string, agentId: string, config: Record<string, unknown>) =>
    fetchWithAuth(
      `/api/v1/agents/${agentId}/config`,
      { method: "PUT", body: JSON.stringify(config) },
      token
    ) as Promise<Agent>,
  enable: (token: string, agentId: string) =>
    fetchWithAuth(
      `/api/v1/agents/${agentId}/enable`,
      { method: "POST" },
      token
    ) as Promise<Agent>,
  disable: (token: string, agentId: string) =>
    fetchWithAuth(
      `/api/v1/agents/${agentId}/disable`,
      { method: "POST" },
      token
    ) as Promise<Agent>,
  getLogs: (token: string, agentId: string) =>
    fetchWithAuth(
      `/api/v1/agents/${agentId}/logs`,
      {},
      token
    ) as Promise<AgentRunLog[]>,
};
