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

import java.util.HashSet;
import java.util.Set;

import jakarta.persistence.AttributeConverter;
import jakarta.persistence.Converter;

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

import lombok.SneakyThrows;

@Converter
public class JSONStoredFileInformationConverter implements AttributeConverter<Set<StoredFileInformation>, String> {

    private final ObjectMapper objectMapper = new ObjectMapper();


    @SneakyThrows
    @Override
    public String convertToDatabaseColumn(Set<StoredFileInformation> dataSet) {

        return objectMapper.writeValueAsString(dataSet);
    }


    @SneakyThrows
    @Override
    public Set<StoredFileInformation> convertToEntityAttribute(String data) {

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

    }

}
