package com.datamate.datamanagement.interfaces.rest;

import com.datamate.common.infrastructure.common.IgnoreResponseWrap;
import com.datamate.common.interfaces.PagedResponse;
import com.datamate.datamanagement.application.KnowledgeItemApplicationService;
import com.datamate.datamanagement.domain.model.knowledge.KnowledgeItem;
import com.datamate.datamanagement.interfaces.converter.KnowledgeConverter;
import com.datamate.datamanagement.interfaces.dto.CreateKnowledgeItemRequest;
import com.datamate.datamanagement.interfaces.dto.ImportKnowledgeItemsRequest;
import com.datamate.datamanagement.interfaces.dto.KnowledgeItemPagingQuery;
import com.datamate.datamanagement.interfaces.dto.KnowledgeItemResponse;
import com.datamate.datamanagement.interfaces.dto.ReplaceKnowledgeItemFileRequest;
import com.datamate.datamanagement.interfaces.dto.UpdateKnowledgeItemRequest;
import com.datamate.datamanagement.interfaces.dto.UploadKnowledgeItemsRequest;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 知识条目 REST 控制器
 */
@Slf4j
@RestController
@RequiredArgsConstructor
@RequestMapping("/data-management/knowledge-sets/{setId}/items")
public class KnowledgeItemController {
    private final KnowledgeItemApplicationService knowledgeItemApplicationService;

    @GetMapping
    public PagedResponse<KnowledgeItemResponse> getKnowledgeItems(@PathVariable("setId") String setId,
                                                                  KnowledgeItemPagingQuery query) {
        return knowledgeItemApplicationService.getKnowledgeItems(setId, query);
    }

    @PostMapping
    public KnowledgeItemResponse createKnowledgeItem(@PathVariable("setId") String setId,
                                                     @RequestBody @Valid CreateKnowledgeItemRequest request) {
        KnowledgeItem knowledgeItem = knowledgeItemApplicationService.createKnowledgeItem(setId, request);
        return KnowledgeConverter.INSTANCE.convertToResponse(knowledgeItem);
    }

    @PostMapping("/import")
    public List<KnowledgeItemResponse> importKnowledgeItems(@PathVariable("setId") String setId,
                                                            @RequestBody @Valid ImportKnowledgeItemsRequest request) {
        List<KnowledgeItem> items = knowledgeItemApplicationService.importKnowledgeItems(setId, request);
        return KnowledgeConverter.INSTANCE.convertItemResponses(items);
    }

    @PostMapping(value = "/upload", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    public List<KnowledgeItemResponse> uploadKnowledgeItems(@PathVariable("setId") String setId,
                                                            @Valid UploadKnowledgeItemsRequest request) {
        List<KnowledgeItem> items = knowledgeItemApplicationService.uploadKnowledgeItems(setId, request);
        return KnowledgeConverter.INSTANCE.convertItemResponses(items);
    }

    @IgnoreResponseWrap
    @GetMapping("/export")
    public void exportKnowledgeItems(@PathVariable("setId") String setId, HttpServletResponse response) {
        knowledgeItemApplicationService.exportKnowledgeItems(setId, response);
    }

    @IgnoreResponseWrap
    @GetMapping("/{itemId}/file")
    public void downloadKnowledgeItemFile(@PathVariable("setId") String setId,
                                          @PathVariable("itemId") String itemId,
                                          HttpServletResponse response) {
        knowledgeItemApplicationService.downloadKnowledgeItemFile(setId, itemId, response);
    }

    @IgnoreResponseWrap
    @GetMapping("/{itemId}/preview")
    public void previewKnowledgeItemFile(@PathVariable("setId") String setId,
                                         @PathVariable("itemId") String itemId,
                                         HttpServletResponse response) {
        knowledgeItemApplicationService.previewKnowledgeItemFile(setId, itemId, response);
    }

    @GetMapping("/{itemId}")
    public KnowledgeItemResponse getKnowledgeItemById(@PathVariable("setId") String setId,
                                                      @PathVariable("itemId") String itemId) {
        KnowledgeItem knowledgeItem = knowledgeItemApplicationService.getKnowledgeItem(setId, itemId);
        return KnowledgeConverter.INSTANCE.convertToResponse(knowledgeItem);
    }

    @PutMapping("/{itemId}")
    public KnowledgeItemResponse updateKnowledgeItem(@PathVariable("setId") String setId,
                                                     @PathVariable("itemId") String itemId,
                                                     @RequestBody @Valid UpdateKnowledgeItemRequest request) {
        KnowledgeItem knowledgeItem = knowledgeItemApplicationService.updateKnowledgeItem(setId, itemId, request);
        return KnowledgeConverter.INSTANCE.convertToResponse(knowledgeItem);
    }

    @PutMapping(value = "/{itemId}/file", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    public KnowledgeItemResponse replaceKnowledgeItemFile(@PathVariable("setId") String setId,
                                                          @PathVariable("itemId") String itemId,
                                                          @Valid ReplaceKnowledgeItemFileRequest request) {
        KnowledgeItem knowledgeItem = knowledgeItemApplicationService.replaceKnowledgeItemFile(setId, itemId, request);
        return KnowledgeConverter.INSTANCE.convertToResponse(knowledgeItem);
    }

    @DeleteMapping("/{itemId}")
    public void deleteKnowledgeItem(@PathVariable("setId") String setId,
                                    @PathVariable("itemId") String itemId) {
        knowledgeItemApplicationService.deleteKnowledgeItem(setId, itemId);
    }
}
