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.PromptTemplateBo;
import com.emoon.knowledge.domain.vo.PromptTemplateVo;
import com.emoon.knowledge.service.IPromptTemplateService;
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-25
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/system/template")
public class PromptTemplateController extends BaseController {

    private final IPromptTemplateService promptTemplateService;

    /**
     * 查询提示词模板（生产级）列表
     */
    @SaCheckPermission("system:template:list")
    @GetMapping("/list")
    public TableDataInfo<PromptTemplateVo> list(PromptTemplateBo bo, PageQuery pageQuery) {
        return promptTemplateService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出提示词模板（生产级）列表
     */
    @SaCheckPermission("system:template:export")
    @Log(title = "提示词模板（生产级）", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(PromptTemplateBo bo, HttpServletResponse response) {
        List<PromptTemplateVo> list = promptTemplateService.queryList(bo);
        ExcelUtil.exportExcel(list, "提示词模板（生产级）", PromptTemplateVo.class, response);
    }

    /**
     * 获取提示词模板（生产级）详细信息
     *
     * @param id 主键
     */
    @SaCheckPermission("system:template:query")
    @GetMapping("/{id}")
    public R<PromptTemplateVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long id) {
        return R.ok(promptTemplateService.queryById(id));
    }

    /**
     * 新增提示词模板（生产级）
     */
    @SaCheckPermission("system:template:add")
    @Log(title = "提示词模板（生产级）", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody PromptTemplateBo bo) {
        return toAjax(promptTemplateService.insertByBo(bo));
    }

    /**
     * 修改提示词模板（生产级）
     */
    @SaCheckPermission("system:template:edit")
    @Log(title = "提示词模板（生产级）", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody PromptTemplateBo bo) {
        return toAjax(promptTemplateService.updateByBo(bo));
    }

    /**
     * 删除提示词模板（生产级）
     *
     * @param ids 主键串
     */
    @SaCheckPermission("system:template:remove")
    @Log(title = "提示词模板（生产级）", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] ids) {
        return toAjax(promptTemplateService.deleteWithValidByIds(List.of(ids), true));
    }
}
