import { apiClient } from "./client";

const BASE = "/ai-agents";

export const aiAgentApi = {
  // 에이전트 CRUD
  list: (params?: { status?: string; search?: string }) =>
    apiClient.get(BASE, { params }).then((r) => r.data),
  getById: (id: number) =>
    apiClient.get(`${BASE}/${id}`).then((r) => r.data),
  create: (data: any) =>
    apiClient.post(BASE, data).then((r) => r.data),
  update: (id: number, data: any) =>
    apiClient.put(`${BASE}/${id}`, data).then((r) => r.data),
  delete: (id: number) =>
    apiClient.delete(`${BASE}/${id}`).then((r) => r.data),

  // API 키 관리
  listKeys: () =>
    apiClient.get(`${BASE}/keys/list`).then((r) => r.data),
  createKey: (data: { name: string; agent_id?: number; rate_limit?: number }) =>
    apiClient.post(`${BASE}/keys`, data).then((r) => r.data),
  revokeKey: (id: number) =>
    apiClient.delete(`${BASE}/keys/${id}`).then((r) => r.data),

  // LLM 프로바이더
  listProviders: () =>
    apiClient.get(`${BASE}/providers/list`).then((r) => r.data),
  createProvider: (data: any) =>
    apiClient.post(`${BASE}/providers`, data).then((r) => r.data),
  updateProvider: (id: number, data: any) =>
    apiClient.put(`${BASE}/providers/${id}`, data).then((r) => r.data),
  deleteProvider: (id: number) =>
    apiClient.delete(`${BASE}/providers/${id}`).then((r) => r.data),

  // 대화 모니터링
  listConversations: (params?: { page?: number; limit?: number; agent_id?: number }) =>
    apiClient.get(`${BASE}/conversations/list`, { params }).then((r) => r.data),
  getConversation: (id: number) =>
    apiClient.get(`${BASE}/conversations/${id}`).then((r) => r.data),

  // 사용량
  usageSummary: () =>
    apiClient.get(`${BASE}/usage/summary`).then((r) => r.data),
  usageLogs: (params?: { page?: number; limit?: number }) =>
    apiClient.get(`${BASE}/usage/logs`, { params }).then((r) => r.data),
  usageDaily: (days?: number) =>
    apiClient.get(`${BASE}/usage/daily`, { params: { days } }).then((r) => r.data),

  // 멀티 에이전트 그룹
  listGroups: () =>
    apiClient.get("/ai-agent-groups").then((r) => r.data),
  getGroup: (id: number) =>
    apiClient.get(`/ai-agent-groups/${id}`).then((r) => r.data),
  createGroup: (data: { name: string; description?: string }) =>
    apiClient.post("/ai-agent-groups", data).then((r) => r.data),
  updateGroup: (id: number, data: any) =>
    apiClient.put(`/ai-agent-groups/${id}`, data).then((r) => r.data),
  deleteGroup: (id: number) =>
    apiClient.delete(`/ai-agent-groups/${id}`).then((r) => r.data),
  addGroupMember: (groupId: number, data: any) =>
    apiClient.post(`/ai-agent-groups/${groupId}/members`, data).then((r) => r.data),
  updateGroupMember: (memberId: number, data: any) =>
    apiClient.put(`/ai-agent-groups/members/${memberId}`, data).then((r) => r.data),
  removeGroupMember: (memberId: number) =>
    apiClient.delete(`/ai-agent-groups/members/${memberId}`).then((r) => r.data),
  getAvailableConnectors: () =>
    apiClient.get("/ai-agent-groups/connectors").then((r) => r.data),

  // 지식 파일 라이브러리
  listKnowledge: (params?: { category?: string; search?: string }) =>
    apiClient.get("/ai-knowledge", { params }).then((r) => r.data),
  getKnowledge: (id: number) =>
    apiClient.get(`/ai-knowledge/${id}`).then((r) => r.data),
  createKnowledge: (data: { name: string; file_name: string; category: string; description?: string; content: string }) =>
    apiClient.post("/ai-knowledge", data).then((r) => r.data),
  updateKnowledge: (id: number, data: any) =>
    apiClient.put(`/ai-knowledge/${id}`, data).then((r) => r.data),
  deleteKnowledge: (id: number) =>
    apiClient.delete(`/ai-knowledge/${id}`).then((r) => r.data),
};
