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

import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import cn.iocoder.yudao.module.llm.service.http.vo.TextToImageReqVo;
import com.alibaba.fastjson.JSON;
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.conversation.vo.*;
import cn.iocoder.yudao.module.llm.dal.dataobject.conversation.ConversationDO;
import cn.iocoder.yudao.module.llm.service.conversation.ConversationService;

@Tag(name = "管理后台 - 大模型对话记录")
@RestController
@RequestMapping("/llm/conversation")
@Validated
public class ConversationController {

    @Resource
    private ConversationService conversationService;

    @PostMapping("/create")
    @Operation(summary = "创建大模型对话记录")
    @PreAuthorize("@ss.hasPermission('llm:conversation:create')")
    public CommonResult<Integer> createConversation(@Valid @RequestBody ConversationSaveReqVO createReqVO) {
        return success(conversationService.createConversation(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新大模型对话记录")
    @PreAuthorize("@ss.hasPermission('llm:conversation:update')")
    public CommonResult<Boolean> updateConversation(@Valid @RequestBody ConversationSaveReqVO updateReqVO) {
        conversationService.updateConversation(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除大模型对话记录")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('llm:conversation:delete')")
    public CommonResult<Boolean> deleteConversation(@RequestParam("id") Integer id) {
        conversationService.deleteConversation(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得大模型对话记录")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('llm:conversation:query')")
    public CommonResult<ConversationRespVO> getConversation(@RequestParam("id") Integer id) {
        ConversationDO conversation = conversationService.getConversation(id);
        return success(BeanUtils.toBean(conversation, ConversationRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得大模型对话记录分页")
    @PreAuthorize("@ss.hasPermission('llm:conversation:query')")
    public CommonResult<PageResult<ConversationRespVO>> getConversationPage(@Valid ConversationPageReqVO pageReqVO) {
        PageResult<ConversationDO> pageResult = conversationService.getConversationPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, ConversationRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出大模型对话记录 Excel")
    @PreAuthorize("@ss.hasPermission('llm:conversation:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportConversationExcel(@Valid ConversationPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<ConversationDO> list = conversationService.getConversationPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "大模型对话记录.xls", "数据", ConversationRespVO.class,
                        BeanUtils.toBean(list, ConversationRespVO.class));
    }

    @PostMapping("/chat")
    @Operation(summary = "对话推理接口")
    public CommonResult<ChatRespVO> chat(@Valid @RequestBody ChatReqVO chatReqVO) {
        return success(conversationService.chat(chatReqVO));
    }
    @PostMapping("/text-to-image")
    @Operation(summary = "文字转图片接口")
    public CommonResult<JSONArray> textToImage(@Valid @RequestBody TextToImageReqVo req) {
        return success(conversationService.textToImage(req));
    }
}