package com.storlead.knowledge.api;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.storlead.framework.common.result.Result;
import com.storlead.knowledge.config.DifyProperties;
import com.storlead.knowledge.pojo.dto.KnowledgeDTO;
import com.storlead.knowledge.utils.HttpService;
import com.storlead.knowledge.utils.JacksonHolder;
import com.storlead.knowledge.pojo.dto.QueryPageDTO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Map;


@RestController
@RequestMapping("/knowledge/base/")
@Api(tags = "知识库: 知识库管理")
public class KnowledgeController {
    @Resource
    private HttpService httpService;
    @Resource
    private DifyProperties difyProperties;

    @GetMapping("datasets")
    @ApiOperation("获取所有知识库数据集")
    public Result<Object> listDatasets(QueryPageDTO page) {
        String url = difyProperties.getBaseUrl() + "datasets";
        return httpService.get(
                url,
                page.getKeyword()==null?Map.of("page", page.getPageIndex(), "size", page.getPageSize()):Map.of("page", page.getPageIndex(), "size", page.getPageSize(), "keyword", page.getKeyword()),
                "Bearer "+difyProperties.getDatasetApiKey(),
                new TypeReference<>() {}
        );
    }

    @GetMapping("datasets/{dataset_id}")
    @ApiOperation("获取指定ID的知识库")
    public Result<Object> getDataset(@PathVariable String dataset_id ) {
        String url = difyProperties.getBaseUrl() + "datasets/"+dataset_id;
        return httpService.get(
                url,
                null,
                "Bearer "+difyProperties.getDatasetApiKey(),
                new TypeReference<>() {});
    }

    @DeleteMapping("datasets/{dataset_id}")
    @ApiOperation("删除指定ID的知识库")
    public Result<Object> deleteDataset(@PathVariable String  dataset_id) {
        String url = difyProperties.getBaseUrl() + "datasets/"+dataset_id;
        return httpService.delete(
                url,
                null,
                "Bearer "+difyProperties.getDatasetApiKey(),
                new TypeReference<>() {}) ;
    }

    @PostMapping("datasets")
    @ApiOperation("创建知识库")
    public Result<Object> createDataset(@RequestBody KnowledgeDTO knowledgeDTO) throws JsonProcessingException {
        String url = difyProperties.getBaseUrl() + "datasets";
        String body = JacksonHolder.OBJECT_MAPPER.writeValueAsString(knowledgeDTO);
        return httpService.post(url, null, "Bearer "+difyProperties.getDatasetApiKey(), body, new TypeReference<>() {});
    }

    @PatchMapping("datasets/{dataset_id}")
    @ApiOperation("更新指定ID的知识库")
    public Result<Object> updateDataset(@RequestBody KnowledgeDTO knowledgeDTO, @PathVariable String  dataset_id) throws JsonProcessingException {
        String url = difyProperties.getBaseUrl() + "datasets/"+dataset_id;
        String body = JacksonHolder.OBJECT_MAPPER.writeValueAsString(knowledgeDTO);
        return httpService.patch(url, null, "Bearer "+difyProperties.getDatasetApiKey(), body, new TypeReference<>() {});
    }

    @PostMapping("datasets/{dataset_id}/retrieve")
    @ApiOperation("知识库测试")
    public Result<Object> retrieve( @PathVariable String  dataset_id,@RequestBody  QueryPageDTO queryPageDTO) throws JsonProcessingException {
        String url = difyProperties.getBaseUrl() + "datasets/"+dataset_id+"/retrieve";
        String body = JacksonHolder.OBJECT_MAPPER.writeValueAsString(queryPageDTO);
        return httpService.post(url, null, "Bearer "+difyProperties.getDatasetApiKey(), body, new TypeReference<>() {});
    }

    @GetMapping("workspaces/current/models/model-types/{model_type}")
    @ApiOperation("获取可用模型")
    public Result<Object> retrieve( @PathVariable String  model_type) {
        String url = difyProperties.getBaseUrl() + "workspaces/current/models/model-types/"+model_type;
        return httpService.get(url, null, "Bearer "+difyProperties.getDatasetApiKey(), new TypeReference<>() {});
    }
}
