package com.supervision.chat.controller;

import cn.hutool.json.JSONUtil;
import com.supervision.chat.client.CustomMultipartFile;
import com.supervision.chat.client.LangChainChatService;
import com.supervision.chat.client.dto.CreateBaseDTO;
import com.supervision.chat.client.dto.DeleteFileDTO;
import com.supervision.chat.client.dto.LangChainChatRes;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;

@Slf4j
@RequestMapping("chat/test/")
@RestController
@RequiredArgsConstructor
public class TestController {

    private final LangChainChatService langChainChatClient;

    @GetMapping("test")
    public void test() {
        CreateBaseDTO createBaseDTO = new CreateBaseDTO();
        createBaseDTO.setKnowledge_base_name("11111111");
        LangChainChatRes chat = langChainChatClient.createBase(createBaseDTO);
        log.info(JSONUtil.toJsonStr(chat));
    }

    @PostMapping("uploadFile")
    public void testUploadFile(@RequestPart("file") MultipartFile file) throws IOException {
        CustomMultipartFile mockMultipartFile = new CustomMultipartFile(file.getOriginalFilename(), file.getInputStream());
        LangChainChatRes langChainChatRes = langChainChatClient.uploadFile("11111111",
                mockMultipartFile,
                "问讯笔录",
                "true",
                "false",
                "false",
                250,
                50,
                "false",
                "{\"test.txt\":[{\"page_content\":\"custom doc\",\"metadata\":{},\"type\":\"Document\"}]}");
        log.info(JSONUtil.toJsonStr(langChainChatRes));
    }

    @GetMapping("deleteFile")
    public void testDeleteFile(String knowledgeBaseName, String fileName) {
        LangChainChatRes langChainChatRes = langChainChatClient.deleteFile(DeleteFileDTO.create(knowledgeBaseName, fileName));
        log.info(JSONUtil.toJsonStr(langChainChatRes));
    }

}