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

import com.alibaba.fastjson.JSONObject;
import com.cy.guoyan.admin.module.ai.controller.admin.chatknowledgedataset.vo.ChatKnowledgeDatasetPageReqVO;
import com.cy.guoyan.admin.module.ai.controller.admin.chatknowledgedataset.vo.ChatKnowledgeDatasetRespVO;
import com.cy.guoyan.admin.module.ai.controller.admin.chatknowledgedataset.vo.ChatKnowledgeDatasetSaveReqVO;
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.chatknowledgedataset.ChatKnowledgeDatasetDO;
import com.cy.guoyan.admin.module.ai.service.chatknowledgedataset.ChatKnowledgeDatasetService;

@Tag(name = "管理后台 - AI 知识库")
@RestController
@RequestMapping("/chatAi/chat-knowledge-dataset")
@Validated
public class ChatKnowledgeDatasetController {

    @Resource
    private ChatKnowledgeDatasetService chatKnowledgeDatasetService;

    @PostMapping("/create")
    @Operation(summary = "创建AI 知识库")
    @PreAuthorize("@ss.hasPermission('chatAi:chat-knowledge-dataset:create')")
    public CommonResult<Long> createChatKnowledgeDataset(@Valid @RequestBody ChatKnowledgeDatasetSaveReqVO createReqVO)throws IOException {
        return success(chatKnowledgeDatasetService.createChatKnowledgeDataset(createReqVO));
    }

    @PostMapping("/retrieveFromDataset")
    @Operation(summary = "检索知识库内容")
    @PreAuthorize("@ss.hasPermission('chatAi:chat-knowledge-dataset:create')")
    public CommonResult<JSONObject> retrieveFromDataset(@RequestBody ChatKnowledgeDatasetSaveReqVO createReqVO)throws IOException {
        return success(chatKnowledgeDatasetService.retrieveFromDataset(createReqVO));
    }


    @GetMapping("/sync-list")
    @Operation(summary = "同步已有 Dify 知识库列表")
    @PreAuthorize("@ss.hasPermission('chatAi:chat-knowledge-dataset:create')")
    public CommonResult<Integer> syncKnowledgeDatasets() throws IOException {
        int count = chatKnowledgeDatasetService.syncFromDify();
        return success(count);
    }


    @PutMapping("/update")
    @Operation(summary = "更新AI 知识库")
    @PreAuthorize("@ss.hasPermission('chatAi:chat-knowledge-dataset:update')")
    public CommonResult<Boolean> updateChatKnowledgeDataset(@Valid @RequestBody ChatKnowledgeDatasetSaveReqVO updateReqVO) {
        chatKnowledgeDatasetService.updateChatKnowledgeDataset(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除AI 知识库")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('chatAi:chat-knowledge-dataset:delete')")
    public CommonResult<Boolean> deleteChatKnowledgeDataset(@RequestParam("id") Long id) throws IOException {
        chatKnowledgeDatasetService.deleteChatKnowledgeDataset(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得AI 知识库")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('chatAi:chat-knowledge-dataset:query')")
    public CommonResult<ChatKnowledgeDatasetRespVO> getChatKnowledgeDataset(@RequestParam("id") Long id) {
        ChatKnowledgeDatasetDO chatKnowledgeDataset = chatKnowledgeDatasetService.getChatKnowledgeDataset(id);
        return success(BeanUtils.toBean(chatKnowledgeDataset, ChatKnowledgeDatasetRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得AI 知识库分页")
    @PreAuthorize("@ss.hasPermission('chatAi:chat-knowledge-dataset:query')")
    public CommonResult<PageResult<ChatKnowledgeDatasetRespVO>> getChatKnowledgeDatasetPage(@Valid ChatKnowledgeDatasetPageReqVO pageReqVO) {
        PageResult<ChatKnowledgeDatasetDO> pageResult = chatKnowledgeDatasetService.getChatKnowledgeDatasetPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, ChatKnowledgeDatasetRespVO.class));
    }

    @GetMapping("/list")
    @Operation(summary = "获得AI 知识库列表")
    @PreAuthorize("@ss.hasPermission('chatAi:chat-knowledge-dataset:query')")
    public CommonResult<List<ChatKnowledgeDatasetRespVO>> getChatKnowledgeDatasetList( ChatKnowledgeDatasetPageReqVO pageReqVO) {
        List<ChatKnowledgeDatasetDO> pageResult = chatKnowledgeDatasetService.getChatKnowledgeDatasetList(pageReqVO);
        return success(BeanUtils.toBean(pageResult, ChatKnowledgeDatasetRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出AI 知识库 Excel")
    @PreAuthorize("@ss.hasPermission('chatAi:chat-knowledge-dataset:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportChatKnowledgeDatasetExcel(@Valid ChatKnowledgeDatasetPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<ChatKnowledgeDatasetDO> list = chatKnowledgeDatasetService.getChatKnowledgeDatasetPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "AI 知识库.xls", "数据", ChatKnowledgeDatasetRespVO.class,
                        BeanUtils.toBean(list, ChatKnowledgeDatasetRespVO.class));
    }

}