package com.ruoyi.web.llm.controller;

import com.alibaba.fastjson2.JSONArray;
import com.ruoyi.llm.service.ILangChainMilvusService;
import com.ruoyi.llm.service.IMilvusService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;

/**
 * cmc知识库Controller
 *
 * @author cmc
 * @date 2025-04-08
 */
@RestController
@RequestMapping("/llm/knowledge")
public class KnowLedgeController extends BaseController
{
    @Autowired
    private IMilvusService milvusService;

    @Autowired
    private ILangChainMilvusService langChainMilvusService;

    /**
     * 新建知识库
     */
    @GetMapping("/list")
    public AjaxResult listKnowLedgeCollection()
    {
        JSONArray collectionNames = milvusService.getCollectionNames();
        return success(collectionNames);
    }
    /**
     * 新建知识库
     */
    @GetMapping("/listByName")
    public AjaxResult listKnowLedgeByCollectionName(String collectionName)
    {
        JSONArray collectionNames = milvusService.listKnowLedgeByCollectionName(collectionName);
        return success(collectionNames);
    }

    /**
     * 新建知识库
     */
    @PostMapping("/create")
    public AjaxResult createKnowLedgeCollection(String collectionName, String description)
    {
        milvusService.createCollection(collectionName, description, 512);
        return success();
    }

    /**
     * 修改知识库
     */
    @PostMapping("/modify")
    public AjaxResult modifyKnowLedgeCollection(String collectionName, String newCollectionName)
    {
        milvusService.collectionRename(collectionName, newCollectionName);
        return success();
    }

    /**
     * 删除知识库集合
     */
    @DeleteMapping("/remove")
    public AjaxResult removeKnowLedgeCollection(String collectionName)
    {
        milvusService.removeAllDocument(collectionName);
        milvusService.deleteCollectionName(collectionName);
        return success();
    }

    /**
     * 查询知识库文件
     */
    @GetMapping("/listDocument")
    public AjaxResult listKnowledgeDocument(String collectionName, String fileType)
    {
        List<String> documentList = milvusService.listDocument(collectionName, fileType);
        return success(documentList);
    }

    /**
     * 获取上传进度
     */
    @GetMapping(value = "/getProcess")
    public AjaxResult getProcess() {
        return success(langChainMilvusService.getProcess());
    }

    /**
     * 导入知识库文件
     */
    @PostMapping("/insertDocument")
    public AjaxResult insertKnowledgeDocument(MultipartFile[] fileList, String collectionName) throws IOException {
        int insertResult = 0;
        insertResult = langChainMilvusService.insertLangchainEmbeddingDocument(fileList, collectionName);
        String message = "文件导入成功";
        if (insertResult == 0) {
            message = "文件导入失败";
            return error(message);
        }
        else
            return success(message);
    }

    /**
     * 删除知识库文件
     */
    @DeleteMapping("/removeDocument")
    public AjaxResult deleteKnowledgeDocument(String fileName, String collectionName)
    {
        milvusService.removeDocument(collectionName, fileName);
        return success();
    }

    /**
     * 列出所有的title
     */
    @GetMapping("/listTiles")
    public AjaxResult listTiles(String collectionName)
    {
        List<String> titles = milvusService.listTiles(collectionName);
        return success(titles);
    }

    /**
     * 根据title名查询相关content
     */
    @GetMapping("/listByTitle")
    public AjaxResult listByTitle(String collectionName, String title)
    {
        JSONArray contents = milvusService.listByTitle(collectionName, title);
        return success(contents);
    }

    /**
     * 根据文件名获取标题列表
     */
    @GetMapping("/listTilesByFile")
    public AjaxResult listTilesByFile(String collectionName, String fileName)
    {
        List<String> titles = milvusService.listTilesByFile(collectionName, fileName);
        return success(titles);
    }

}
