package org.jk.rag.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import jakarta.servlet.http.HttpServletResponse;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.jk.common.core.domain.R;
import org.jk.common.excel.utils.ExcelUtil;
import org.jk.common.idempotent.annotation.RepeatSubmit;
import org.jk.common.log.annotation.Log;
import org.jk.common.log.enums.BusinessType;
import org.jk.common.mybatis.core.page.PageQuery;
import org.jk.common.mybatis.core.page.TableDataInfo;
import org.jk.common.web.core.BaseController;
import org.jk.rag.domain.entity.*;
import org.jk.rag.domain.vo.KnowledgeSliceVo;
import org.jk.rag.service.IJkRagService;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.util.*;

@Slf4j
@RestController
@RequiredArgsConstructor
@RequestMapping("/deepseek/api")
public class JkRagKnowledgeController extends BaseController {


    private final IJkRagService iJkRagService;


    /**
     * 获取知识库下拉列表
     *
     * @return
     */
    @GetMapping("/knowledgeList")
    public R<List<JkKnowledge>> getKnowledgeList() {
        return R.ok(iJkRagService.queryKnowledgeList());
    }


    /**
     * 知识库列表
     *
     * @param params
     * @return
     */
    @PostMapping("/getKnowledgeList")
    public TableDataInfo getKnowledgeList(@RequestBody JkPageParams params,@RequestBody PageQuery pageQuery) {
        Page<JkKnowledge> list = iJkRagService.queryKnowledgePageList(params,pageQuery);
        return TableDataInfo.build(list);
    }

    /**
     * 创建知识库
     *
     * @param knowledge
     * @return
     */
    @SaCheckPermission("knowledge:knowledgeLib:add")
    @PostMapping("/createKnowledge")
    @RepeatSubmit
    public R<Integer> createKnowledge(@RequestBody JkKnowledgeParams knowledge) {
        int i = iJkRagService.createKnowledge(knowledge);
        return R.ok(i);
    }

    /**
     * 知识库详情
     *
     * @param knowledgeId
     * @return
     */
    //@SaCheckPermission("knowledge:knowledgeLib:detail")
    @GetMapping("/detailKnowledge/{knowledgeId}")
    public R<JkKnowledge> detailKnowledge(@PathVariable String knowledgeId) {
        JkKnowledge result = iJkRagService.detailKnowledge(knowledgeId);
        return R.ok(result);
    }

    /**
     * 修改知识库
     *
     * @param knowledge
     * @param knowledgeId
     * @return
     */
    //@SaCheckPermission("knowledge:knowledgeLib:edit")
    @PutMapping("/updateKnowledge/{knowledgeId}")
    @RepeatSubmit
    public R<Integer> updateKnowledge(@RequestBody JkKnowledgeParams knowledge, @PathVariable String knowledgeId) {
        int i = iJkRagService.updateKnowledge(knowledge, knowledgeId);
        return R.ok(i);
    }

    /**
     * 删除知识库
     *
     * @param knowledgeId
     * @return
     */
    //@SaCheckPermission("knowledge:knowledgeLib:del")
    @DeleteMapping("/delKnowledge/{knowledgeId}")
    public R<Integer> delKnowledge(@PathVariable String knowledgeId) {
        int i = iJkRagService.delKnowledge(knowledgeId);
        return R.ok(i);
    }

    //@SaCheckPermission("knowledge:knowledgeLib:downloadMd")
    @Log(title = "下载知识库MarkDown文件", businessType = BusinessType.EXPORT)
    @PostMapping("/knowledgeMarkDownFileExport/{knowledgeId}")
    public void export(HttpServletResponse response, @PathVariable String knowledgeId) throws IOException {
        // 设置响应头
        response.setContentType("application/octet-stream");
        response.setCharacterEncoding("UTF-8");
        response.setHeader("Content-Disposition", "attachment; filename=\"documents.zip\"");
        // 使用 StreamingResponseBody 来实现流式响应
        iJkRagService.downloadKnowledgeMarkDownFileDocument(knowledgeId ,response.getOutputStream());
    }

    //@SaCheckPermission("knowledge:knowledgeLib:downloadOriginal")
    @Log(title = "下载知识库原文档", businessType = BusinessType.EXPORT)
    @PostMapping("/knowledgeFileExport/{knowledgeId}")
    public void  knowledgeFileExport(HttpServletResponse response, @PathVariable String knowledgeId) throws IOException {

        // 设置响应头
        response.setContentType("application/octet-stream");
        response.setCharacterEncoding("UTF-8");
        response.setHeader("Content-Disposition", "attachment; filename=\"documents.zip\"");

        // 使用 StreamingResponseBody 来实现流式响应
        iJkRagService.downloadKnowledgeFileDocument(knowledgeId ,response.getOutputStream());

    }


}
