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

namespace TakeoutSaaS.Application.App.Stores.Validators;

/// <summary>
/// 创建门店资质命令验证器。
/// </summary>
public sealed class CreateStoreQualificationCommandValidator : AbstractValidator<CreateStoreQualificationCommand>
{
    /// <summary>
    /// 初始化验证规则。
    /// </summary>
    public CreateStoreQualificationCommandValidator()
    {
        RuleFor(x => x.StoreId).GreaterThan(0);
        RuleFor(x => x.QualificationType).IsInEnum();
        RuleFor(x => x.FileUrl).NotEmpty().MaximumLength(500);
        RuleFor(x => x.SortOrder).GreaterThanOrEqualTo(0);
        RuleFor(x => x.DocumentNumber).MaximumLength(100);

        RuleFor(x => x.ExpiresAt)
            .Must(date => date.HasValue && date.Value > DateOnly.FromDateTime(DateTime.UtcNow))
            .When(x => IsLicenseType(x.QualificationType))
            .WithMessage("证照有效期必须晚于今天");

        RuleFor(x => x.DocumentNumber)
            .NotEmpty()
            .MinimumLength(2)
            .When(x => IsLicenseType(x.QualificationType))
            .WithMessage("证照编号不能为空");

        RuleFor(x => x.ExpiresAt)
            .Must(date => !date.HasValue || date.Value > DateOnly.FromDateTime(DateTime.UtcNow))
            .When(x => !IsLicenseType(x.QualificationType))
            .WithMessage("证照有效期必须晚于今天");
    }

    private static bool IsLicenseType(StoreQualificationType type)
        => type is StoreQualificationType.BusinessLicense or StoreQualificationType.FoodServiceLicense;
}
