package com.iqser.red.service.persistence.management.v1.processor.utils;

import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.iqser.red.service.pdftron.redaction.v1.api.model.RedactionResultDetail;

import jakarta.persistence.AttributeConverter;
import jakarta.persistence.Converter;
import lombok.SneakyThrows;

import java.util.ArrayList;
import java.util.List;

@Converter
public class JSONDownloadRedactionFileDetailsConverter implements AttributeConverter<List<RedactionResultDetail>, String> {

    private final ObjectMapper objectMapper = new ObjectMapper();


    @SneakyThrows
    @Override
    public String convertToDatabaseColumn(List<RedactionResultDetail> dataSet) {

        return objectMapper.writeValueAsString(dataSet);
    }


    @SneakyThrows
    @Override
    public List<RedactionResultDetail> convertToEntityAttribute(String data) {

        if (data == null) {
            return new ArrayList<>();
        }
        TypeReference<ArrayList<RedactionResultDetail>> typeRef = new TypeReference<>() {
        };
        return objectMapper.readValue(data, typeRef);

    }

}
