package ru.vyatsu.qr_access_admin.common.validation;

import jakarta.validation.ConstraintValidator;
import jakarta.validation.ConstraintValidatorContext;

import java.time.LocalDateTime;

public class IsAfterValidator implements ConstraintValidator<IsAfter, LocalDateTime> {

    private LocalDateTime dateTime;

    @Override
    public void initialize(IsAfter constraintAnnotation) {
        String sDate = constraintAnnotation.dateTime();
        if (sDate.equals("NOW")) {
            dateTime = LocalDateTime.now();
        } else {
            dateTime = LocalDateTime.parse(sDate);
        }
    }

    @Override
    public boolean isValid(LocalDateTime value, ConstraintValidatorContext context) {
        if (value == null) return true;
        return value.isAfter(dateTime);
    }
}
