using FluentValidation;
using MembershipService.API.Application.Commands;

namespace MembershipService.API.Application.Validations;

/// <summary>
/// EN: Validator for CreateMemberCommand.
/// VI: Validator cho CreateMemberCommand.
/// </summary>
public class CreateMemberCommandValidator : AbstractValidator<CreateMemberCommand>
{
    public CreateMemberCommandValidator()
    {
        RuleFor(x => x.UserId)
            .NotEmpty()
            .WithMessage("UserId is required");

        RuleFor(x => x.CountryCode)
            .NotEmpty()
            .Length(2)
            .WithMessage("CountryCode must be 2 characters");

        RuleFor(x => x.Gender)
            .MaximumLength(10)
            .Must(BeAValidGender)
            .When(x => !string.IsNullOrEmpty(x.Gender))
            .WithMessage("Gender must be Male, Female, or Other");
    }

    private static bool BeAValidGender(string? gender)
    {
        if (string.IsNullOrEmpty(gender)) return true;
        var validGenders = new[] { "Male", "Female", "Other" };
        return validGenders.Contains(gender, StringComparer.OrdinalIgnoreCase);
    }
}
