package com.datamate.datamanagement.interfaces.rest;

import com.datamate.common.interfaces.PagedResponse;
import com.datamate.datamanagement.application.KnowledgeItemApplicationService;
import com.datamate.datamanagement.application.KnowledgeSetApplicationService;
import com.datamate.datamanagement.domain.model.knowledge.KnowledgeSet;
import com.datamate.datamanagement.interfaces.converter.KnowledgeConverter;
import com.datamate.datamanagement.interfaces.dto.CreateKnowledgeSetRequest;
import com.datamate.datamanagement.interfaces.dto.KnowledgeManagementStatisticsResponse;
import com.datamate.datamanagement.interfaces.dto.KnowledgeSetPagingQuery;
import com.datamate.datamanagement.interfaces.dto.KnowledgeSetResponse;
import com.datamate.datamanagement.interfaces.dto.UpdateKnowledgeSetRequest;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

/**
 * 知识集 REST 控制器
 */
@Slf4j
@RestController
@RequiredArgsConstructor
@RequestMapping("/data-management/knowledge-sets")
public class KnowledgeSetController {
    private final KnowledgeSetApplicationService knowledgeSetApplicationService;
    private final KnowledgeItemApplicationService knowledgeItemApplicationService;

    @GetMapping
    public PagedResponse<KnowledgeSetResponse> getKnowledgeSets(KnowledgeSetPagingQuery query) {
        return knowledgeSetApplicationService.getKnowledgeSets(query);
    }

    @PostMapping
    public KnowledgeSetResponse createKnowledgeSet(@RequestBody @Valid CreateKnowledgeSetRequest request) {
        KnowledgeSet knowledgeSet = knowledgeSetApplicationService.createKnowledgeSet(request);
        return KnowledgeConverter.INSTANCE.convertToResponse(knowledgeSet);
    }

    @GetMapping("/{setId}")
    public KnowledgeSetResponse getKnowledgeSetById(@PathVariable("setId") String setId) {
        KnowledgeSet knowledgeSet = knowledgeSetApplicationService.getKnowledgeSet(setId);
        return KnowledgeConverter.INSTANCE.convertToResponse(knowledgeSet);
    }

    @PutMapping("/{setId}")
    public KnowledgeSetResponse updateKnowledgeSet(@PathVariable("setId") String setId,
                                                   @RequestBody @Valid UpdateKnowledgeSetRequest request) {
        KnowledgeSet knowledgeSet = knowledgeSetApplicationService.updateKnowledgeSet(setId, request);
        return KnowledgeConverter.INSTANCE.convertToResponse(knowledgeSet);
    }

    @DeleteMapping("/{setId}")
    public void deleteKnowledgeSet(@PathVariable("setId") String setId) {
        knowledgeSetApplicationService.deleteKnowledgeSet(setId);
    }

    @GetMapping("/statistics")
    public KnowledgeManagementStatisticsResponse getKnowledgeManagementStatistics() {
        return knowledgeItemApplicationService.getKnowledgeManagementStatistics();
    }
}
