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

namespace TakeoutSaaS.Application.App.Products.Validators;

/// <summary>
/// 保存分类命令验证器。
/// </summary>
public sealed class SaveProductCategoryCommandValidator : AbstractValidator<SaveProductCategoryCommand>
{
    /// <summary>
    /// 初始化验证规则。
    /// </summary>
    public SaveProductCategoryCommandValidator()
    {
        RuleFor(x => x.StoreId).GreaterThan(0);
        RuleFor(x => x.CategoryId).GreaterThan(0).When(x => x.CategoryId.HasValue);
        RuleFor(x => x.Name).NotEmpty().MaximumLength(64);
        RuleFor(x => x.Description).MaximumLength(256);
        RuleFor(x => x.Icon).MaximumLength(512);
        RuleFor(x => x.Sort).GreaterThan(0);
        RuleFor(x => x.Status)
            .Must(ProductCategoryManageMapping.IsValidStatus)
            .WithMessage("status 必须为 enabled 或 disabled");

        RuleFor(x => x.Channels)
            .NotEmpty()
            .WithMessage("channels 至少选择一个渠道");
        RuleForEach(x => x.Channels)
            .Must(ProductCategoryManageMapping.IsValidChannel)
            .WithMessage("channels 包含非法值");
    }
}
