using FluentValidation;
using IWS5.Core.Models.Request;
using IWS5.DatabaseAccess.Interfaces;

namespace IWS5.Services.Validators;

public class RefreshTokenRequestValidator : AbstractValidator<RefreshTokenRequest>
{
    public RefreshTokenRequestValidator(IUserRepository userRepository)
    {
        RuleFor(x => x.UserName)
            .NotEmpty()
            .WithMessage("User name is required and should not be empty.")
            .DependentRules(() =>
            {
                RuleFor(x => x.UserName)
                    .Must(userName =>
                        userRepository.AnyAsync(user => user.UserName.ToLower() == userName.ToLower()).Result)
                    .WithMessage("User with the specified user name does not exist.");
            });

        RuleFor(x => x.RefreshToken)
            .NotEmpty()
            .WithMessage("Refresh token is required and should not be empty.");
    }
}