using FluentValidation;
using Indotalent.Shared.Consts;

namespace Indotalent.Features.Pipeline.Budget.Cqrs;

public class CreateBudgetValidator : AbstractValidator<CreateBudgetRequest>
{
    public CreateBudgetValidator()
    {
        RuleFor(x => x.Title)
            .NotEmpty().WithMessage("Title is required")
            .MaximumLength(GlobalConsts.StringLengthShort);

        RuleFor(x => x.CampaignId)
            .NotEmpty().WithMessage("Campaign is required");

        RuleFor(x => x.Amount)
            .GreaterThanOrEqualTo(0).WithMessage("Amount must be zero or greater");
    }

    public Func<object, string, Task<IEnumerable<string>>> ValidateValue() => async (model, propertyName) =>
    {
        var result = await ValidateAsync(ValidationContext<CreateBudgetRequest>.CreateWithOptions((CreateBudgetRequest)model, x => x.IncludeProperties(propertyName)));
        if (result.IsValid) return Array.Empty<string>();
        return result.Errors.Select(e => e.ErrorMessage);
    };
}