package com.datamate.datamanagement.interfaces.rest;

import com.datamate.datamanagement.application.KnowledgeDirectoryApplicationService;
import com.datamate.datamanagement.domain.model.knowledge.KnowledgeItemDirectory;
import com.datamate.datamanagement.interfaces.converter.KnowledgeConverter;
import com.datamate.datamanagement.interfaces.dto.CreateKnowledgeDirectoryRequest;
import com.datamate.datamanagement.interfaces.dto.KnowledgeDirectoryQuery;
import com.datamate.datamanagement.interfaces.dto.KnowledgeDirectoryResponse;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 知识条目目录 REST 控制器
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/data-management/knowledge-sets/{setId}/directories")
public class KnowledgeDirectoryController {
    private final KnowledgeDirectoryApplicationService knowledgeDirectoryApplicationService;

    @GetMapping
    public List<KnowledgeDirectoryResponse> getKnowledgeDirectories(@PathVariable("setId") String setId,
                                                                   KnowledgeDirectoryQuery query) {
        List<KnowledgeItemDirectory> directories = knowledgeDirectoryApplicationService.getKnowledgeDirectories(setId, query);
        return KnowledgeConverter.INSTANCE.convertDirectoryResponses(directories);
    }

    @PostMapping
    public KnowledgeDirectoryResponse createKnowledgeDirectory(@PathVariable("setId") String setId,
                                                              @RequestBody @Valid CreateKnowledgeDirectoryRequest request) {
        KnowledgeItemDirectory directory = knowledgeDirectoryApplicationService.createKnowledgeDirectory(setId, request);
        return KnowledgeConverter.INSTANCE.convertToResponse(directory);
    }

    @DeleteMapping
    public void deleteKnowledgeDirectory(@PathVariable("setId") String setId,
                                         @RequestParam("relativePath") String relativePath) {
        knowledgeDirectoryApplicationService.deleteKnowledgeDirectory(setId, relativePath);
    }
}
