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

import com.njcn.gather.icd.mapping.pojo.bo.GenerateMappingResult;
import com.njcn.gather.icd.mapping.pojo.bo.analysis.IndexCandidate;
import com.njcn.gather.icd.mapping.pojo.bo.analysis.IndexCandidateReportItem;
import com.njcn.gather.icd.mapping.pojo.enums.GenerateStatus;
import com.njcn.gather.icd.mapping.pojo.vo.IndexCandidateReportItemResponse;
import com.njcn.gather.icd.mapping.pojo.vo.IndexCandidateResponse;
import com.njcn.gather.icd.mapping.pojo.vo.MappingTaskResponse;
import org.springframework.stereotype.Component;

/**
 * 接口响应转换器。
 *
 * 按接口阶段仅组装当前场景必需字段，避免返回冗余信息。
 */
@Component
public class MappingResponseConverter {

    /**
     * 候选接口响应。
     */
    public MappingTaskResponse fromCandidateResult(GenerateMappingResult result) {
        MappingTaskResponse response = initBaseResponse(result);
        response.setIcdDocument(result.getIcdDocument());
        fillIndexCandidates(response, result);
        return response;
    }

    /**
     * 正式提交类接口响应。
     *
     * SUCCESS：仅返回最终结果；
     * NEED_INDEX_SELECTION：返回重新选择所需的候选信息；
     * FAILED：仅返回错误信息。
     */
    public MappingTaskResponse fromSubmitResult(GenerateMappingResult result) {
        MappingTaskResponse response = initBaseResponse(result);
        if (result.getStatus() == GenerateStatus.SUCCESS) {
            response.setMappingJson(result.getMappingJson());
            response.setMappingDocument(result.getMappingDocument());
            response.setSavedPath(result.getSavedPath());
            return response;
        }

        if (result.getStatus() == GenerateStatus.NEED_INDEX_SELECTION) {
            response.setIcdDocument(result.getIcdDocument());
            fillIndexCandidates(response, result);
        }
        return response;
    }

    private MappingTaskResponse initBaseResponse(GenerateMappingResult result) {
        MappingTaskResponse response = new MappingTaskResponse();
        response.setStatus(result.getStatus());
        response.setMessage(result.getMessage());
        response.getProblems().addAll(result.getProblems());
        return response;
    }

    private void fillIndexCandidates(MappingTaskResponse response, GenerateMappingResult result) {
        if (result.getIndexAnalysis() == null || result.getIndexAnalysis().getCandidates() == null) {
            return;
        }
        for (IndexCandidate candidate : result.getIndexAnalysis().getCandidates()) {
            IndexCandidateResponse candidateResponse = new IndexCandidateResponse();
            candidateResponse.setGroupKey(candidate.getGroupKey());
            candidateResponse.setGroupDesc(candidate.getGroupDesc());
            candidateResponse.setReportCount(candidate.getReportCount());
            candidateResponse.getTemplateLabels().addAll(candidate.getTemplateLabels());

            if (candidate.getReports() != null) {
                for (IndexCandidateReportItem item : candidate.getReports()) {
                    IndexCandidateReportItemResponse itemResponse = new IndexCandidateReportItemResponse();
                    itemResponse.setReportName(item.getReportName());
                    itemResponse.setDataSetName(item.getDataSetName());
                    itemResponse.setReportDesc(item.getReportDesc());
                    itemResponse.getAvailableLnInstValues().addAll(item.getAvailableLnInstValues());
                    candidateResponse.getReports().add(itemResponse);
                }
            }

            response.getIndexCandidates().add(candidateResponse);
        }
    }
}
