using FluentValidation;
using Indotalent.Shared.Consts;

namespace Indotalent.Features.Pipeline.Lead.Cqrs;

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

        RuleFor(x => x.CompanyName)
            .NotEmpty().WithMessage("Company Name is required")
            .MaximumLength(GlobalConsts.StringLengthShort);

        RuleFor(x => x.CompanyEmail)
            .EmailAddress().When(x => !string.IsNullOrEmpty(x.CompanyEmail));

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

        RuleFor(x => x.SalesTeamId)
            .NotEmpty().WithMessage("Sales Team is required");
    }

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