using System.Globalization;
using Baya.Domain.Entities.Catalog;
using FluentValidation;

namespace Baya.Application.Features.Variants.Commands.UpdateVariant;

public sealed class UpdateVariantCommandValidator : AbstractValidator<UpdateVariantCommand>
{
    // Id is supplied by the route (set after model binding), so it is not validated here.
    public UpdateVariantCommandValidator()
    {
        RuleFor(x => x.Price)
            .NotEmpty()
            .Must(BePositiveIrrAmount)
            .WithMessage("Price must be a positive integer number of IRR Rials (digits only).");

        RuleFor(x => x.PriceUnit)
            .Must(PriceUnits.IsValid)
            .WithMessage("price_unit must be one of: per_hour, per_session, per_half_day, per_day, per_24h.");

        RuleFor(x => x.SessionCount).GreaterThan(0).When(x => x.SessionCount.HasValue);

        RuleFor(x => x.DisplayName).MaximumLength(300);
    }

    private static bool BePositiveIrrAmount(string value)
        => long.TryParse(value, NumberStyles.None, CultureInfo.InvariantCulture, out var amount) && amount > 0;
}
