package com.emoon.knowledge.controller;

import java.util.List;

import com.emoon.common.core.domain.R;
import com.emoon.common.core.validate.validate.AddGroup;
import com.emoon.common.core.validate.validate.EditGroup;
import com.emoon.common.excel.utils.ExcelUtil;
import com.emoon.common.idempotent.annotation.RepeatSubmit;
import com.emoon.common.log.annotation.Log;
import com.emoon.common.log.enums.BusinessType;
import com.emoon.common.mybatis.core.page.PageQuery;
import com.emoon.common.mybatis.core.page.TableDataInfo;
import com.emoon.common.web.core.BaseController;
import com.emoon.knowledge.domain.bo.AiAgentBo;
import com.emoon.knowledge.domain.vo.AiAgentVo;
import com.emoon.knowledge.service.IAiAgentService;
import lombok.RequiredArgsConstructor;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.*;
import cn.dev33.satoken.annotation.SaCheckPermission;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;

/**
 * 智能体管理
 *
 * @author wangkang
 * @date 2026-01-27
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/system/agent")
public class AiAgentController extends BaseController {

    private final IAiAgentService aiAgentService;

    /**
     * 查询智能体列表
     */
    @SaCheckPermission("system:agent:list")
    @GetMapping("/list")
    public TableDataInfo<AiAgentVo> list(AiAgentBo bo, PageQuery pageQuery) {
        return aiAgentService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出智能体列表
     */
    @SaCheckPermission("system:agent:export")
    @Log(title = "智能体", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(AiAgentBo bo, HttpServletResponse response) {
        List<AiAgentVo> list = aiAgentService.queryList(bo);
        ExcelUtil.exportExcel(list, "智能体", AiAgentVo.class, response);
    }

    /**
     * 获取智能体详细信息
     *
     * @param id 主键
     */
    @SaCheckPermission("system:agent:query")
    @GetMapping("/{id}")
    public R<AiAgentVo> getInfo(@NotNull(message = "主键不能为空")
                                @PathVariable Long id) {
        return R.ok(aiAgentService.queryById(id));
    }

    /**
     * 根据agentId获取智能体详细信息
     *
     * @param agentId 智能体唯一标识
     */
    @SaCheckPermission("system:agent:query")
    @GetMapping("/agentId/{agentId}")
    public R<AiAgentVo> getInfoByAgentId(@NotBlank(message = "智能体ID不能为空")
                                         @PathVariable String agentId) {
        return R.ok(aiAgentService.queryByAgentId(agentId));
    }

    /**
     * 新增智能体
     */
    @SaCheckPermission("system:agent:add")
    @Log(title = "智能体", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody AiAgentBo bo) {
        return toAjax(aiAgentService.insertByBo(bo));
    }

    /**
     * 修改智能体
     */
    @SaCheckPermission("system:agent:edit")
    @Log(title = "智能体", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody AiAgentBo bo) {
        return toAjax(aiAgentService.updateByBo(bo));
    }

    /**
     * 删除智能体
     *
     * @param ids 主键串
     */
    @SaCheckPermission("system:agent:remove")
    @Log(title = "智能体", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] ids) {
        return toAjax(aiAgentService.deleteWithValidByIds(List.of(ids), true));
    }
}



