package org.dromara.system.controller.ai;

import cn.dev33.satoken.annotation.SaCheckPermission;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.NotEmpty;
import jakarta.validation.constraints.NotNull;
import lombok.RequiredArgsConstructor;
import org.dromara.common.core.domain.R;
import org.dromara.common.core.validate.AddGroup;
import org.dromara.common.core.validate.EditGroup;
import org.dromara.common.excel.utils.ExcelUtil;
import org.dromara.common.idempotent.annotation.RepeatSubmit;
import org.dromara.common.log.annotation.Log;
import org.dromara.common.log.enums.BusinessType;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.common.web.core.BaseController;
import org.dromara.system.domain.bo.AiMcpBo;
import org.dromara.system.domain.vo.AiMcpVo;
import org.dromara.system.service.IAiMcpService;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 模型上下文协议
 *
 * @author wangkang
 * @date 2025-09-19
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/system/mcp")
public class AiMcpController extends BaseController {

    private final IAiMcpService aiMcpService;

    /**
     * 查询模型上下文协议列表
     */
    @SaCheckPermission("system:mcp:list")
    @GetMapping("/list")
    public TableDataInfo<AiMcpVo> list(AiMcpBo bo, PageQuery pageQuery) {
        return aiMcpService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出模型上下文协议列表
     */
    @SaCheckPermission("system:mcp:export")
    @Log(title = "模型上下文协议", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(AiMcpBo bo, HttpServletResponse response) {
        List<AiMcpVo> list = aiMcpService.queryList(bo);
        ExcelUtil.exportExcel(list, "模型上下文协议", AiMcpVo.class, response);
    }

    /**
     * 获取模型上下文协议详细信息
     *
     * @param id 主键
     */
    @SaCheckPermission("system:mcp:query")
    @GetMapping("/{id}")
    public R<AiMcpVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long id) {
        return R.ok(aiMcpService.queryById(id));
    }

    /**
     * 新增模型上下文协议
     */
    @SaCheckPermission("system:mcp:add")
    @Log(title = "模型上下文协议", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody AiMcpBo bo) {
        return toAjax(aiMcpService.insertByBo(bo));
    }

    /**
     * 修改模型上下文协议
     */
    @SaCheckPermission("system:mcp:edit")
    @Log(title = "模型上下文协议", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody AiMcpBo bo) {
        return toAjax(aiMcpService.updateByBo(bo));
    }

    /**
     * 删除模型上下文协议
     *
     * @param ids 主键串
     */
    @SaCheckPermission("system:mcp:remove")
    @Log(title = "模型上下文协议", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] ids) {
        return toAjax(aiMcpService.deleteWithValidByIds(List.of(ids), true));
    }
}
