package org.octopus.internal.db.converters

import jakarta.persistence.AttributeConverter
import jakarta.persistence.Converter
import java.time.Month

@Converter
class MonthListConverter : AttributeConverter<List<Month>, String> {

    override fun convertToDatabaseColumn(attribute: List<Month>?): String? {
        return attribute?.joinToString(separator = ",") { it.name }
    }

    override fun convertToEntityAttribute(dbData: String?): List<Month>? {
        return dbData?.split(",")
            ?.filter { it.isNotBlank() }
            ?.mapNotNull { runCatching { Month.valueOf(it.trim()) }.getOrNull() }
    }
}