package com.kamco.cd.training.common.utils.enums;

import com.kamco.cd.training.common.utils.interfaces.EnumValid;
import jakarta.validation.ConstraintValidator;
import jakarta.validation.ConstraintValidatorContext;
import java.util.Arrays;
import java.util.Set;
import java.util.stream.Collectors;

public class EnumValidator implements ConstraintValidator<EnumValid, String> {

  private Set<String> acceptedValues;

  @Override
  public void initialize(EnumValid constraintAnnotation) {
    acceptedValues =
        Arrays.stream(constraintAnnotation.enumClass().getEnumConstants())
            .map(Enum::name)
            .collect(Collectors.toSet());
  }

  @Override
  public boolean isValid(String value, ConstraintValidatorContext context) {
    return value != null && acceptedValues.contains(value);
  }
}
