package com.cy.guoyan.admin.module.ai.controller.admin.chatsession;

import com.cy.guoyan.admin.module.ai.controller.admin.chatsession.vo.ChatSessionPageReqVO;
import com.cy.guoyan.admin.module.ai.controller.admin.chatsession.vo.ChatSessionRespVO;
import com.cy.guoyan.admin.module.ai.controller.admin.chatsession.vo.ChatSessionSaveReqVO;
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.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;
import com.cy.guoyan.admin.framework.common.pojo.PageParam;
import com.cy.guoyan.admin.framework.common.pojo.PageResult;
import com.cy.guoyan.admin.framework.common.pojo.CommonResult;
import com.cy.guoyan.admin.framework.common.util.object.BeanUtils;
import static com.cy.guoyan.admin.framework.common.pojo.CommonResult.success;
import com.cy.guoyan.admin.framework.excel.core.util.ExcelUtils;
import com.cy.guoyan.admin.framework.apilog.core.annotation.ApiAccessLog;
import static com.cy.guoyan.admin.framework.apilog.core.enums.OperateTypeEnum.*;
import com.cy.guoyan.admin.module.ai.dal.dataobject.chatsession.ChatSessionDO;
import com.cy.guoyan.admin.module.ai.service.chatsession.ChatSessionService;

@Tag(name = "管理后台 - AI聊天会话")
@RestController
@RequestMapping("/chatAi/chat-session")
@Validated
public class ChatSessionController {

    @Resource
    private ChatSessionService chatSessionService;

    @PostMapping("/create")
    @Operation(summary = "创建AI聊天会话")
    @PreAuthorize("@ss.hasPermission('chatAi:chat-session:create')")
    public CommonResult<Long> createChatSession(@Valid @RequestBody ChatSessionSaveReqVO createReqVO) {
        return success(chatSessionService.createChatSession(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新AI聊天会话")
    @PreAuthorize("@ss.hasPermission('chatAi:chat-session:update')")
    public CommonResult<Boolean> updateChatSession(@Valid @RequestBody ChatSessionSaveReqVO updateReqVO) {
        chatSessionService.updateChatSession(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除AI聊天会话")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('chatAi:chat-session:delete')")
    public CommonResult<Boolean> deleteChatSession(@RequestParam("id") Long id) throws IOException {
        chatSessionService.deleteChatSession(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得AI聊天会话")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('chatAi:chat-session:query')")
    public CommonResult<ChatSessionRespVO> getChatSession(@RequestParam("id") Long id) {
        ChatSessionDO chatSession = chatSessionService.getChatSession(id);
        return success(BeanUtils.toBean(chatSession, ChatSessionRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得AI聊天会话分页")
    @PreAuthorize("@ss.hasPermission('chatAi:chat-session:query')")
    public CommonResult<PageResult<ChatSessionRespVO>> getChatSessionPage(@Valid ChatSessionPageReqVO pageReqVO) {
        PageResult<ChatSessionDO> pageResult = chatSessionService.getChatSessionPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, ChatSessionRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出AI聊天会话 Excel")
    @PreAuthorize("@ss.hasPermission('chatAi:chat-session:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportChatSessionExcel(@Valid ChatSessionPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<ChatSessionDO> list = chatSessionService.getChatSessionPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "AI聊天会话.xls", "数据", ChatSessionRespVO.class,
                        BeanUtils.toBean(list, ChatSessionRespVO.class));
    }

}