package com.njcn.gather.icd.mapping.controller;

import com.njcn.common.pojo.annotation.OperateInfo;
import com.njcn.common.pojo.enums.common.LogEnum;
import com.njcn.common.utils.LogUtil;
import com.njcn.gather.icd.mapping.component.MappingRequestConverter;
import com.njcn.gather.icd.mapping.component.MappingResponseConverter;
import com.njcn.gather.icd.mapping.pojo.bo.GenerateMappingResult;
import com.njcn.gather.icd.mapping.pojo.dto.GenerateFromIcdCommand;
import com.njcn.gather.icd.mapping.pojo.param.GenerateMappingFromIcdRequest;
import com.njcn.gather.icd.mapping.pojo.param.SubmitIndexSelectionRequest;
import com.njcn.gather.icd.mapping.pojo.vo.MappingTaskResponse;
import com.njcn.gather.icd.mapping.service.MappingTaskService;
import com.njcn.gather.icd.mapping.utils.DateUtils;
import com.njcn.web.controller.BaseController;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestPart;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

/**
 * ICD 映射接口入口。
 */
@Slf4j
@Api(tags = "ICD 映射")
@RestController
@RequestMapping("/api/mms-mapping")
@RequiredArgsConstructor
public class MappingController extends BaseController {

    /** 映射任务编排服务，负责从 ICD 解析到映射生成的完整链路。 */
    private final MappingTaskService mappingTaskService;

    /** 请求参数转换器，将接口入参转换为应用层命令。 */
    private final MappingRequestConverter requestConverter;

    /** 响应转换器，按接口阶段裁剪最小返回字段。 */
    private final MappingResponseConverter responseConverter;

    /**
     * 上传 ICD 文件，返回候选结果和可编辑的 ICD 解析结果。
     */
    @OperateInfo(info = LogEnum.BUSINESS_COMMON)
    @ApiOperation("上传 ICD 文件并生成索引候选")
    @PostMapping(value = "/get-icd", consumes = {"multipart/form-data"})
    public MappingTaskResponse getICD(@RequestPart("icdFile") MultipartFile icdFile) {
        String methodDescribe = getMethodDescribe("getICD");
        LogUtil.njcnDebug(log, "{}，开始解析 ICD 文件并生成索引候选，fileName={}", methodDescribe, DateUtils.resolveFileName(icdFile));
        GenerateFromIcdCommand command = requestConverter.toCommand(icdFile);
        GenerateMappingResult result = mappingTaskService.getICD(command);
        return responseConverter.fromCandidateResult(result);
    }

    /**
     * 根据前端确认后的索引绑定关系生成 MMS JSON。
     */
    @OperateInfo(info = LogEnum.BUSINESS_COMMON)
    @ApiOperation("获取 MMS JSON")
    @PostMapping("/get-mms-json")
    public MappingTaskResponse getMmsJson(@Validated @RequestBody SubmitIndexSelectionRequest request) {
        String methodDescribe = getMethodDescribe("getMmsJson");
        LogUtil.njcnDebug(log, "{}，开始生成 MMS JSON，indexSelectionCount={}",
                methodDescribe, DateUtils.resolveSelectionCount(request == null ? null : request.getIndexSelection()));
        GenerateFromIcdCommand command = requestConverter.toCommand(request);
        GenerateMappingResult result = mappingTaskService.getMmsJson(command);
        return responseConverter.fromSubmitResult(result);
    }

    /**
     * 上传 ICD 后直接串联候选生成和索引提交，统一返回正式提交阶段结果。
     */
    @OperateInfo(info = LogEnum.BUSINESS_COMMON)
    @ApiOperation("上传 ICD 后直接获取 MMS JSON")
    @PostMapping(value = "/get-icd-mms-json", consumes = {"multipart/form-data"})
    public MappingTaskResponse getIcdMmsJson(@RequestPart("icdFile") MultipartFile icdFile,
                                             @Validated @RequestPart("request") GenerateMappingFromIcdRequest request) {
        String methodDescribe = getMethodDescribe("getIcdMmsJson");
        LogUtil.njcnDebug(log, "{}，开始获取 ICD MMS JSON，fileName={}, indexSelectionCount={}",
                methodDescribe, DateUtils.resolveFileName(icdFile),
                DateUtils.resolveSelectionCount(request == null ? null : request.getIndexSelection()));
        GenerateFromIcdCommand command = requestConverter.toCommand(icdFile, request);
        GenerateMappingResult result = mappingTaskService.getIcdMmsJson(command);
        return responseConverter.fromSubmitResult(result);
    }
}
