/**
 * @description 智能体配置相关API
 * @author AI Assistant
 * @since 2025-11-04
 */

import { api } from '@/apis/index';
import type { AiAgentConfig, ResultDomain, PageDomain, PageParam } from '@/types';

/**
 * 智能体配置API服务
 */
export const aiAgentConfigApi = {
  /**
   * 创建智能体
   * @param agentConfig 智能体配置
   * @returns Promise<ResultDomain<AiAgentConfig>>
   */
  async createAgent(agentConfig: AiAgentConfig): Promise<ResultDomain<AiAgentConfig>> {
    const response = await api.post<AiAgentConfig>('/ai/agent', agentConfig);
    return response.data;
  },

  /**
   * 更新智能体
   * @param agentConfig 智能体配置
   * @returns Promise<ResultDomain<AiAgentConfig>>
   */
  async updateAgent(agentConfig: AiAgentConfig): Promise<ResultDomain<AiAgentConfig>> {
    const response = await api.put<AiAgentConfig>('/ai/agent', agentConfig);
    return response.data;
  },

  /**
   * 删除智能体
   * @param agentId 智能体ID
   * @returns Promise<ResultDomain<boolean>>
   */
  async deleteAgent(agentId: string): Promise<ResultDomain<boolean>> {
    const response = await api.delete<boolean>(`/ai/agent/${agentId}`);
    return response.data;
  },

  /**
   * 获取智能体详情
   * @param agentId 智能体ID
   * @returns Promise<ResultDomain<AiAgentConfig>>
   */
  async getAgentById(agentId: string): Promise<ResultDomain<AiAgentConfig>> {
    const response = await api.get<AiAgentConfig>(`/ai/agent/${agentId}`, {
      showLoading: false
    });
    return response.data;
  },

  /**
   * 获取启用的智能体列表
   * @returns Promise<ResultDomain<AiAgentConfig[]>>
   */
  async listEnabledAgents(): Promise<ResultDomain<AiAgentConfig>> {
    const response = await api.get<AiAgentConfig>('/ai/agent/enabled',{}, {
      showLoading: false
    });
    return response.data;
  },

  /**
   * 获取智能体列表（支持过滤）
   * @param filter 过滤条件
   * @returns Promise<ResultDomain<AiAgentConfig[]>>
   */
  async listAgents(filter?: Partial<AiAgentConfig>): Promise<ResultDomain<AiAgentConfig[]>> {
    const response = await api.post<AiAgentConfig[]>('/ai/agent/list', filter || {});
    return response.data;
  },

  /**
   * 分页查询智能体
   * @param filter 过滤条件
   * @param pageParam 分页参数
   * @returns Promise<PageDomain<AiAgentConfig>>
   */
  async pageAgents(filter: Partial<AiAgentConfig>, pageParam: PageParam): Promise<ResultDomain<AiAgentConfig>> {
    const response = await api.post<AiAgentConfig>('/ai/agent/page', {
      filter,
      pageParam
    });
    return response.data;
  },

  /**
   * 更新智能体状态
   * @param agentId 智能体ID
   * @param status 状态（0禁用 1启用）
   * @returns Promise<ResultDomain<boolean>>
   */
  async updateAgentStatus(agentId: string, status: number): Promise<ResultDomain<boolean>> {
    const response = await api.put<boolean>(`/ai/agent/${agentId}/status`, null, {
      params: { status }
    });
    return response.data;
  },

  /**
   * 更新Dify配置
   * @param agentId 智能体ID
   * @param difyAppId Dify应用ID
   * @param difyApiKey Dify API Key
   * @returns Promise<ResultDomain<boolean>>
   */
  async updateDifyConfig(agentId: string, difyAppId: string, difyApiKey: string): Promise<ResultDomain<boolean>> {
    const response = await api.put<boolean>(`/ai/agent/${agentId}/dify`, null, {
      params: { difyAppId, difyApiKey }
    });
    return response.data;
  },

  /**
   * 检查智能体名称是否存在
   * @param name 名称
   * @param excludeId 排除的ID（用于更新时）
   * @returns Promise<ResultDomain<boolean>>
   */
  async checkNameExists(name: string, excludeId?: string): Promise<ResultDomain<boolean>> {
    const response = await api.get<boolean>('/ai/agent/check-name', {
      params: { name, excludeId }
    });
    return response.data;
  }
};
