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.TagDTO;
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;


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

    @Resource
    private DifyProperties difyProperties;

    @GetMapping("list")
    @ApiOperation("获取所有知识库标签")
    public Result<Object> listTags() {
        String url = difyProperties.getBaseUrl() + "datasets/tags";
        return httpService.get(
                url,
                null,
                "Bearer "+difyProperties.getDatasetApiKey(),
                new TypeReference<>() {}
        );
    }

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

    @DeleteMapping("/delete")
    @ApiOperation("删除标签")
    public Result<Object> deleteDataset() {
        String url = difyProperties.getBaseUrl() + "datasets/tags";
        return httpService.delete(
                url,
                null,
                "Bearer "+difyProperties.getDatasetApiKey(),
                new TypeReference<>() {}) ;
    }

    @PostMapping("/create")
    @ApiOperation("创建标签")
    public Result<Object> createTag(@RequestBody TagDTO tagDTO) throws JsonProcessingException {
        String url = difyProperties.getBaseUrl() + "datasets/tags";
        String body = JacksonHolder.OBJECT_MAPPER.writeValueAsString(tagDTO);
        return httpService.post(url, null, "Bearer "+difyProperties.getDatasetApiKey(), body, new TypeReference<>() {});
    }

    @PatchMapping("update")
    @ApiOperation("更新指定ID的标签")
    public Result<Object> updateTag(@RequestBody TagDTO tagDTO) throws JsonProcessingException {
        String url = difyProperties.getBaseUrl() + "datasets/tags";
        String body = JacksonHolder.OBJECT_MAPPER.writeValueAsString(tagDTO);
        return httpService.patch(url, null, "Bearer "+difyProperties.getDatasetApiKey(), body, new TypeReference<>() {});
    }

    @PostMapping("/binding")
    @ApiOperation("将标签绑定到知识库")
    public Result<Object> binding( @RequestBody  TagDTO tagDTO) throws JsonProcessingException {
        String url = difyProperties.getBaseUrl() + "datasets/tags/binding";
        String body = JacksonHolder.OBJECT_MAPPER.writeValueAsString(tagDTO);
        return httpService.post(url, null, "Bearer "+difyProperties.getDatasetApiKey(), body, new TypeReference<>() {});
    }

    @PostMapping("/unbinding")
    @ApiOperation("将标签绑定到知识库")
    public Result<Object> unbinding(@RequestBody  TagDTO tagDTO) throws JsonProcessingException {
        String url = difyProperties.getBaseUrl() + "datasets/tags/unbinding";
        String body = JacksonHolder.OBJECT_MAPPER.writeValueAsString(tagDTO);
        return httpService.post(url, null, "Bearer "+difyProperties.getDatasetApiKey(), body, new TypeReference<>() {});
    }
}
