package org.octopus.internal.web.dtos.validators

import jakarta.validation.Constraint
import jakarta.validation.ConstraintValidator
import jakarta.validation.ConstraintValidatorContext
import jakarta.validation.Payload
import org.octopus.internal.common.enums.EEntryType
import java.time.Month
import kotlin.reflect.KClass


class EntryMonthValidator : ConstraintValidator<EntryMonthValidator.Validate, List<Int>?> {
    override fun isValid(value: List<Int>?, context: ConstraintValidatorContext): Boolean {
        val monthInt = Month.entries.map { m -> m.value }
        if (value == null) {
            return true
        }

        val allMatched = value.all { v -> monthInt.contains(v) }

        if (!allMatched) {
            context.disableDefaultConstraintViolation()
            context.buildConstraintViolationWithTemplate("Invalid. '$value' contains at least a value outside [1,12] range.")
                .addConstraintViolation()
        }

        return allMatched
    }

    @Target(AnnotationTarget.FIELD, AnnotationTarget.PROPERTY, AnnotationTarget.VALUE_PARAMETER)
    @Retention(AnnotationRetention.RUNTIME)
    @MustBeDocumented
    @Constraint(validatedBy = [EntryMonthValidator::class])
    annotation class Validate(
        val message: String = "",
        val groups: Array<KClass<*>> = [],
        val payload: Array<KClass<out Payload>> = []
    )
}