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

import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import com.njcn.gather.icd.mapping.pojo.bo.mapping.MappingDocument;
import org.springframework.stereotype.Component;

/**
 * 映射文档序列化器。
 */
@Component
public class MappingDocumentSerializer {

    /** 紧凑 JSON 输出器，用于减少响应体体积。 */
    private final ObjectMapper compactMapper;

    /** 格式化 JSON 输出器，用于人工查看和落盘审阅。 */
    private final ObjectMapper prettyMapper;

    public MappingDocumentSerializer() {
        compactMapper = new ObjectMapper();
        compactMapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);

        prettyMapper = new ObjectMapper();
        prettyMapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
        prettyMapper.enable(SerializationFeature.INDENT_OUTPUT);
    }

    /**
     * 序列化为紧凑 JSON。
     */
    public String toCompactJson(MappingDocument document) {
        try {
            return compactMapper.writeValueAsString(document);
        } catch (Exception ex) {
            throw new IllegalArgumentException("映射文档序列化失败：" + ex.getMessage(), ex);
        }
    }

    /**
     * 序列化为格式化 JSON。
     */
    public String toPrettyJson(MappingDocument document) {
        try {
            return prettyMapper.writeValueAsString(document);
        } catch (Exception ex) {
            throw new IllegalArgumentException("映射文档序列化失败：" + ex.getMessage(), ex);
        }
    }
}