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

namespace TakeoutSaaS.Application.App.Stores.Validators;

/// <summary>
/// 批量更新营业时段命令验证器。
/// </summary>
public sealed class BatchUpdateBusinessHoursCommandValidator : AbstractValidator<BatchUpdateBusinessHoursCommand>
{
    /// <summary>
    /// 初始化验证规则。
    /// </summary>
    public BatchUpdateBusinessHoursCommandValidator()
    {
        RuleFor(x => x.StoreId).GreaterThan(0);

        RuleForEach(x => x.Items).ChildRules(item =>
        {
            item.RuleFor(x => x.StartTime).NotNull();
            item.RuleFor(x => x.EndTime).NotNull();
            item.RuleFor(x => x.CapacityLimit).GreaterThanOrEqualTo(0).When(x => x.CapacityLimit.HasValue);
            item.RuleFor(x => x.Notes).MaximumLength(256);
        });

        RuleFor(x => x.Items).Custom((items, context) =>
        {
            if (items == null || items.Count == 0)
            {
                return;
            }

            var error = BusinessHourValidators.ValidateOverlap(items);
            if (!string.IsNullOrWhiteSpace(error))
            {
                context.AddFailure(error);
            }
        });
    }
}
