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.persistence.service.v1.api.shared.model.dossiertemplate.DownloadFileType;

import lombok.SneakyThrows;

@Converter
public class JSONDownloadFileTypeConverter implements AttributeConverter<Set<DownloadFileType>, String> {

    private final ObjectMapper objectMapper = new ObjectMapper();


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

        return objectMapper.writeValueAsString(dataSet);
    }


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

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

    }

}
