package com.astalange.core.validation;

import com.astalange.core.entity.Adherent;
import jakarta.validation.ConstraintValidator;
import jakarta.validation.ConstraintValidatorContext;
import java.time.LocalDate;
import java.time.Period;

public class RepresentantLegalValidator implements ConstraintValidator<ValidRepresentantLegal, Adherent> {

    @Override
    public boolean isValid(Adherent adherent, ConstraintValidatorContext context) {
        if (adherent == null || adherent.getDateNaissance() == null) {
            return true; // Let @NotNull handle the null cases
        }

        int age = Period.between(adherent.getDateNaissance(), LocalDate.now()).getYears();

        if (age < 18) {
            return adherent.getRepresentantLegal() != null && !adherent.getRepresentantLegal().trim().isEmpty();
        }

        return true;
    }
}
