package org.xyzh.ai.controller;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.xyzh.api.ai.agent.AiAgentConfigService;
import org.xyzh.common.core.domain.ResultDomain;
import org.xyzh.common.core.page.PageDomain;
import org.xyzh.common.core.page.PageParam;
import org.xyzh.common.core.page.PageRequest;
import org.xyzh.common.dto.ai.TbAiAgentConfig;

import java.util.List;
import java.util.Map;

/**
 * @description AI智能体配置控制器
 * @filename AiAgentConfigController.java
 * @author AI Assistant
 * @copyright xyzh
 * @since 2025-11-04
 */
@Slf4j
@RestController
@RequestMapping("/ai/agent")
public class AiAgentConfigController {

    @Autowired
    private AiAgentConfigService agentConfigService;

    /**
     * @description 创建智能体
     * @param agentConfig 智能体配置
     * @return ResultDomain<TbAiAgentConfig>
     * @author AI Assistant
     * @since 2025-11-04
     */
    @PostMapping
    public ResultDomain<TbAiAgentConfig> createAgent(@RequestBody TbAiAgentConfig agentConfig) {
        log.info("创建智能体: name={}", agentConfig.getName());
        return agentConfigService.createAgent(agentConfig);
    }

    /**
     * @description 更新智能体
     * @param agentConfig 智能体配置
     * @return ResultDomain<TbAiAgentConfig>
     * @author AI Assistant
     * @since 2025-11-04
     */
    @PutMapping
    public ResultDomain<TbAiAgentConfig> updateAgent(@RequestBody TbAiAgentConfig agentConfig) {
        log.info("更新智能体: id={}, name={}", agentConfig.getDifyAppId(), agentConfig.getName());
        return agentConfigService.updateAgent(agentConfig);
    }

    /**
     * @description 删除智能体
     * @param id 智能体ID
     * @return ResultDomain<Boolean>
     * @author AI Assistant
     * @since 2025-11-04
     */
    @DeleteMapping("/{id}")
    public ResultDomain<Boolean> deleteAgent(@PathVariable(name = "id") String id) {
        log.info("删除智能体: id={}", id);
        return agentConfigService.deleteAgent(id);
    }

    /**
     * @description 根据ID获取智能体
     * @param id 智能体ID
     * @return ResultDomain<TbAiAgentConfig>
     * @author AI Assistant
     * @since 2025-11-04
     */
    @GetMapping("/{id}")
    public ResultDomain<TbAiAgentConfig> getAgent(@PathVariable(name = "id") String id) {
        log.info("获取智能体: id={}", id);
        return agentConfigService.getAgentById(id);
    }

    /**
     * @description 获取启用的智能体列表
     * @return ResultDomain<TbAiAgentConfig>
     * @author AI Assistant
     * @since 2025-11-04
     */
    @GetMapping("/enabled")
    public ResultDomain<TbAiAgentConfig> getEnabledAgents() {
        log.info("获取启用的智能体列表");
        return agentConfigService.listEnabledAgents();
    }

    /**
     * @description 查询智能体列表
     * @param agentConfig 智能体配置
     * @return ResultDomain<TbAiAgentConfig>
     * @author AI Assistant
     * @since 2025-11-04
     */
    @PostMapping("/list")
    public ResultDomain<TbAiAgentConfig> listAgents(
            @RequestBody TbAiAgentConfig agentConfig) {
        log.info("查询智能体列表: agentConfig={}", agentConfig);
        return agentConfigService.listAgents(agentConfig);
    }

    /**
     * @description 分页查询智能体
     * @param pageParam 分页参数
     * @return PageDomain<TbAiAgentConfig>
     * @author AI Assistant
     * @since 2025-11-04
     */
    @PostMapping("/page")
    public PageDomain<TbAiAgentConfig> pageAgents(@RequestBody PageRequest<TbAiAgentConfig> pageRequest) {
        log.info("分页查询智能体: pageNum={}, pageSize={}", 
                pageRequest.getPageParam().getPageNumber(), pageRequest.getPageParam().getPageSize());
        return agentConfigService.pageAgents(pageRequest.getFilter(), pageRequest.getPageParam());
    }

    /**
     * @description 更新智能体状态
     * @param requestBody 请求体（id, status）
     * @return ResultDomain<Boolean>
     * @author AI Assistant
     * @since 2025-11-04
     */
    @PutMapping("/status")
    public ResultDomain<Boolean> updateStatus(@RequestBody Map<String, Object> requestBody) {
        String id = (String) requestBody.get("id");
        Integer status = (Integer) requestBody.get("status");
        log.info("更新智能体状态: id={}, status={}", id, status);
        return agentConfigService.updateAgentStatus(id, status);
    }

    /**
     * @description 更新Dify配置
     * @param requestBody 请求体（id, difyAppId, difyApiKey）
     * @return ResultDomain<Boolean>
     * @author AI Assistant
     * @since 2025-11-04
     */
    @PutMapping("/dify")
    public ResultDomain<Boolean> updateDifyConfig(@RequestBody Map<String, Object> requestBody) {
        String id = (String) requestBody.get("id");
        String difyAppId = (String) requestBody.get("difyAppId");
        String difyApiKey = (String) requestBody.get("difyApiKey");
        log.info("更新Dify配置: id={}, difyAppId={}", id, difyAppId);
        return agentConfigService.updateDifyConfig(id, difyAppId, difyApiKey);
    }

    /**
     * @description 检查名称是否存在
     * @param name 智能体名称
     * @param excludeId 排除的ID（更新时使用）
     * @return ResultDomain<Boolean>
     * @author AI Assistant
     * @since 2025-11-04
     */
    @GetMapping("/check-name")
    public ResultDomain<Boolean> checkNameExists(
            @RequestParam(name = "name") String name,
            @RequestParam(name = "excludeId", required = false) String excludeId) {
        log.info("检查名称是否存在: name={}, excludeId={}", name, excludeId);
        return agentConfigService.checkNameExists(name, excludeId);
    }
}

