package com.storlead.system.controller;

import com.storlead.framework.common.result.Result;
import com.storlead.system.pojo.dto.AgentPromptTemplatePageDTO;
import com.storlead.system.pojo.dto.AgentPromptTemplateQueryDTO;
import com.storlead.system.pojo.entity.AgentPromptTemplateEntity;
import com.storlead.system.service.AgentPromptTemplateService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * AI 提示词模板管理
 */
@Api(tags = "平台中心 - 系统: AI提示词模板")
@RestController
@RequestMapping("/sys/agent/prompt")
public class AgentPromptTemplateApiController {

    @Resource
    private AgentPromptTemplateService agentPromptTemplateService;

    @ApiOperation("保存提示词模板")
    @PostMapping("/save")
    public Result<?> save(@RequestBody AgentPromptTemplateEntity body) {
        return agentPromptTemplateService.saveTemplate(body);
    }

    @ApiOperation("获取提示词（按 templateCode）")
    @PostMapping("/getAiPrompt")
    public Result<?> getAiPrompt(@RequestBody AgentPromptTemplateQueryDTO param) {
        return agentPromptTemplateService.getAiPrompt(param);
    }

    @ApiOperation("分页查询")
    @PostMapping("/pagelist")
    public Result<?> pagelist(@RequestBody AgentPromptTemplatePageDTO dto) {
        return Result.result(agentPromptTemplateService.pageQuery(dto));
    }

    @ApiOperation("列表查询（可按编码批量）")
    @PostMapping("/list")
    public Result<?> list(@RequestBody AgentPromptTemplateQueryDTO dto) {
        return Result.result(agentPromptTemplateService.listQuery(dto));
    }

    @ApiOperation("详情")
    @PostMapping("/detail")
    public Result<?> detail(@RequestParam Long id) {
        AgentPromptTemplateEntity one = agentPromptTemplateService.getDetail(id);
        if (one == null) {
            return Result.error("模板不存在或已删除");
        }
        return Result.result(one);
    }

    @ApiOperation("按模板编码查询（启用且未删除）")
    @PostMapping("/getByCode")
    public Result<?> getByCode(@RequestParam String templateCode,
                               @RequestParam(required = false) Long appId) {
        AgentPromptTemplateEntity one = agentPromptTemplateService.getByTemplateCode(templateCode, appId);
        if (one == null) {
            return Result.error("未找到对应的AI提示词模板");
        }
        return Result.result(one);
    }

    @ApiOperation("获取渲染后的 prompt 内容")
    @PostMapping("/getPromptContent")
    public Result<?> getPromptContent(@RequestBody AgentPromptTemplateQueryDTO dto) {
        return agentPromptTemplateService.getPromptContent(dto);
    }

    @ApiOperation("启用/禁用")
    @PostMapping("/enable")
    public Result<?> enable(@RequestParam Long id, @RequestParam Boolean enabled) {
        return agentPromptTemplateService.enableTemplate(id, enabled);
    }

    @ApiOperation("逻辑删除")
    @PostMapping("/delete")
    public Result<?> delete(@RequestParam Long id) {
        return agentPromptTemplateService.deleteTemplate(id);
    }
}
