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.ChunkDTO;
import com.storlead.knowledge.pojo.dto.QueryPageDTO;
import com.storlead.knowledge.utils.HttpService;
import com.storlead.knowledge.utils.JacksonHolder;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

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


@RestController
@RequestMapping("/knowledge/chunk/")
@Api(tags = "知识库: 文档文本块")
public class ChunkController {
    @Resource
    private HttpService httpService;
    @Resource
    private DifyProperties difyProperties;


    @GetMapping("{dataset_id}/{document_id}")
    @ApiOperation("获取指定知识库指定文档文本块")
    public Result<Object> list(@PathVariable String dataset_id , @PathVariable String document_id , QueryPageDTO page) {
        String url = difyProperties.getBaseUrl()  + "datasets/"+ dataset_id +"/documents/"+document_id +"/segments";
        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("{dataset_id}/{document_id}/{segment_id}")
    @ApiOperation("获取指定知识库指定文档文本块")
    public Result<Object> getOne(@PathVariable String dataset_id , @PathVariable String document_id , @PathVariable String segment_id ) {
        String url = difyProperties.getBaseUrl()  + "datasets/"+ dataset_id +"/documents/"+document_id +"/segments/"+segment_id;
        return httpService.get(
                url,
                null,
                "Bearer "+difyProperties.getDatasetApiKey(),
                new TypeReference<>() {}
        );
    }


    @DeleteMapping("{dataset_id}/{document_id}/{segment_id}")
    @ApiOperation("删除文本块")
    public Result<Object> deleteDataset(@PathVariable String dataset_id , @PathVariable String document_id , @PathVariable String segment_id ) {
        String url = difyProperties.getBaseUrl()  + "datasets/"+ dataset_id +"/documents/"+document_id +"/segments/"+segment_id;
        return httpService.delete(
                url,
                null,
                "Bearer "+difyProperties.getDatasetApiKey(),
                new TypeReference<>() {}) ;
    }

    @PostMapping("/{dataset_id}/{document_id}")
    @ApiOperation("创建文本块")
    public Result<?> createSegments(
            @PathVariable String dataset_id,
            @PathVariable String document_id,
            @RequestBody ChunkDTO request
    ) throws JsonProcessingException {
        String url = difyProperties.getBaseUrl()  + "datasets/"+ dataset_id +"/documents/"+document_id +"/segments/";
        return  httpService.post(
                url,
                Map.of(),
                "Bearer "+difyProperties.getDatasetApiKey(),
                JacksonHolder.OBJECT_MAPPER.writeValueAsString(request),
                new TypeReference<>() {}
        );

    }
    @PostMapping("/{dataset_id}/{document_id}/{segment_id}")
    @ApiOperation("更新指定ID的文本块")
    public Result<Object> updateChunk(  @PathVariable String dataset_id,
                                        @PathVariable String document_id,
                                        @PathVariable String segment_id,
                                        @RequestBody ChunkDTO request) throws JsonProcessingException {
        String url = difyProperties.getBaseUrl()  + "datasets/"+ dataset_id +"/documents/"+document_id +"/segments/"+segment_id;
        return   httpService.post(
                url,
                Map.of(),
                "Bearer "+difyProperties.getDatasetApiKey(),
                JacksonHolder.OBJECT_MAPPER.writeValueAsString(request),
                new TypeReference<>() {}
        );


    }

}
