package cn.iocoder.yudao.module.llm.controller.admin.prompttemplates;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.llm.controller.admin.prompttemplates.vo.*;
import cn.iocoder.yudao.module.llm.dal.dataobject.prompttemplates.PromptTemplatesDO;
import cn.iocoder.yudao.module.llm.service.prompttemplates.PromptTemplatesService;

@Tag(name = "管理后台 - Prompt模板")
@RestController
@RequestMapping("/llm/prompt-templates")
@Validated
public class PromptTemplatesController {

    @Resource
    private PromptTemplatesService promptTemplatesService;

    @PostMapping("/create")
    @Operation(summary = "创建Prompt模板")
    @PreAuthorize("@ss.hasPermission('llm:prompt-templates:create')")
    public CommonResult<Long> createPromptTemplates(@Valid @RequestBody PromptTemplatesSaveReqVO createReqVO) {
        return success(promptTemplatesService.createPromptTemplates(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新Prompt模板")
    @PreAuthorize("@ss.hasPermission('llm:prompt-templates:update')")
    public CommonResult<Boolean> updatePromptTemplates(@Valid @RequestBody PromptTemplatesSaveReqVO updateReqVO) {
        promptTemplatesService.updatePromptTemplates(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除Prompt模板")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('llm:prompt-templates:delete')")
    public CommonResult<Boolean> deletePromptTemplates(@RequestParam("id") Long id) {
        promptTemplatesService.deletePromptTemplates(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "Prompt模板详情")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('llm:prompt-templates:query')")
    public CommonResult<PromptTemplatesRespVO> getPromptTemplates(@RequestParam("id") Long id) {
        return success(promptTemplatesService.getPromptTemplates(id));
    }

    @GetMapping("/page")
    @Operation(summary = "分页查询Prompt模板")
    @PreAuthorize("@ss.hasPermission('llm:prompt-templates:query')")
    public CommonResult<PageResult<PromptTemplatesRespVO>> getPromptTemplatesPage(@Valid PromptTemplatesPageReqVO pageReqVO) {
        return success(promptTemplatesService.getPromptTemplatesPage(pageReqVO));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出Prompt模板")
    @PreAuthorize("@ss.hasPermission('llm:prompt-templates:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportPromptTemplatesExcel(@Valid PromptTemplatesPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<PromptTemplatesRespVO> list = promptTemplatesService.getPromptTemplatesPage(pageReqVO).getList();
        List<PromptTemplatesExcelVO> excelList = BeanUtils.toBean(list, PromptTemplatesExcelVO.class);
        for(PromptTemplatesExcelVO excelVO : excelList){
            // TODO 从字典模块查询
            excelVO.setTemplateCategoryDisplay("");
            excelVO.setTag("");
        }
        // 导出 Excel
        ExcelUtils.write(response, "Prompt模板信息.xls", "数据",
                PromptTemplatesExcelVO.class, excelList);
    }

}
