using FluentValidation;
using TakeoutSaaS.Application.App.Products.Commands;

namespace TakeoutSaaS.Application.App.Products.Validators;

/// <summary>
/// 替换加料验证器。
/// </summary>
public sealed class ReplaceProductAddonsCommandValidator : AbstractValidator<ReplaceProductAddonsCommand>
{
    /// <summary>
    /// 初始化验证规则。
    /// </summary>
    public ReplaceProductAddonsCommandValidator()
    {
        RuleFor(x => x.ProductId).GreaterThan(0);
        RuleForEach(x => x.AddonGroups).ChildRules(group =>
        {
            group.RuleFor(x => x.Name).NotEmpty().MaximumLength(64);
            group.RuleFor(x => x.MinSelect).GreaterThanOrEqualTo(0);
            group.RuleFor(x => x.MaxSelect).GreaterThanOrEqualTo(x => x.MinSelect);
            group.RuleFor(x => x.SortOrder).GreaterThanOrEqualTo(0);
            group.RuleForEach(x => x.Options).ChildRules(opt =>
            {
                opt.RuleFor(x => x.Name).NotEmpty().MaximumLength(64);
                opt.RuleFor(x => x.ExtraPrice).GreaterThanOrEqualTo(0).When(x => x.ExtraPrice.HasValue);
                opt.RuleFor(x => x.SortOrder).GreaterThanOrEqualTo(0);
            });
        });
    }
}
