using FluentValidation;

namespace Baya.Application.Features.Refunds.Commands.CreateRefund;

public sealed class CreateRefundCommandValidator : AbstractValidator<CreateRefundCommand>
{
    public CreateRefundCommandValidator()
    {
        RuleFor(x => x.BookingId).GreaterThan(0);

        When(x => x.RefundPercentage.HasValue, () =>
        {
            RuleFor(x => x.RefundPercentage!.Value)
                .GreaterThan(0m).LessThanOrEqualTo(1m)
                .WithMessage("refund_percentage must be a fraction in (0, 1].");
        });

        // Explicit legs are all-or-nothing and non-negative; the handler enforces amount = fee + payout and the
        // Σ refunded ≤ captured invariant under the booking refund lock.
        When(x => x.PlatformFeeRefundedIrr.HasValue || x.NursePayoutRefundedIrr.HasValue, () =>
        {
            RuleFor(x => x.PlatformFeeRefundedIrr)
                .NotNull().GreaterThanOrEqualTo(0)
                .WithMessage("Both refund legs must be supplied together.");
            RuleFor(x => x.NursePayoutRefundedIrr)
                .NotNull().GreaterThanOrEqualTo(0)
                .WithMessage("Both refund legs must be supplied together.");
        });

        RuleFor(x => x.ReasonCategory).MaximumLength(50);
        RuleFor(x => x.ReasonNotes).MaximumLength(1000);
        RuleFor(x => x.AdminNotes).MaximumLength(1000);
        RuleFor(x => x.ManualBankReference).MaximumLength(200);
    }
}
