using FluentValidation;

namespace Baya.Application.Features.PartnerCenters.Commands.CreatePartnerCenter;

public sealed class CreatePartnerCenterCommandValidator : AbstractValidator<CreatePartnerCenterCommand>
{
    public CreatePartnerCenterCommandValidator()
    {
        RuleFor(x => x.Name).NotEmpty().MaximumLength(300);
        RuleFor(x => x.LegalEntityType).MaximumLength(30);
        RuleFor(x => x.MohEstablishmentPermitNo).NotEmpty().MaximumLength(100);
        RuleFor(x => x.TechnicalDirectorLicenseNo).MaximumLength(100);
        RuleFor(x => x.EnamadCode).MaximumLength(100);
        RuleFor(x => x.AdminUserId).GreaterThan(0);

        // The center's cut is a fraction in [0, 1) — never ≥ 1, never negative.
        RuleFor(x => x.CommissionRate)
            .InclusiveBetween(0m, 0.9999m)
            .When(x => x.CommissionRate.HasValue);

        // A merchant-of-record center is the settlement target, so it MUST carry a settlement IBAN.
        RuleFor(x => x.SettlementIban)
            .NotEmpty()
            .MaximumLength(34)
            .When(x => x.IsMerchantOfRecord)
            .WithMessage("A merchant-of-record center requires a settlement IBAN.");

        RuleFor(x => x.SettlementIban).MaximumLength(34).When(x => !x.IsMerchantOfRecord);
    }
}
