// EN: Validators for Merchant commands.
// VI: Validators cho các commands Merchant.

using FluentValidation;
using MerchantService.API.Application.Commands.Merchants;

namespace MerchantService.API.Application.Validations;

/// <summary>
/// EN: Validator for RegisterMerchantCommand.
/// VI: Validator cho RegisterMerchantCommand.
/// </summary>
public class RegisterMerchantCommandValidator : AbstractValidator<RegisterMerchantCommand>
{
    public RegisterMerchantCommandValidator()
    {
        RuleFor(x => x.BusinessName)
            .NotEmpty().WithMessage("Business name is required")
            .MaximumLength(200).WithMessage("Business name cannot exceed 200 characters");

        RuleFor(x => x.Type)
            .NotEmpty().WithMessage("Merchant type is required")
            .Must(BeValidMerchantType).WithMessage("Invalid merchant type. Valid values: Individual, Company");

        RuleFor(x => x.TaxId)
            .MaximumLength(20).WithMessage("Tax ID cannot exceed 20 characters")
            .When(x => !string.IsNullOrEmpty(x.TaxId));

        RuleFor(x => x.BusinessLicenseNumber)
            .MaximumLength(50).WithMessage("Business license number cannot exceed 50 characters")
            .When(x => !string.IsNullOrEmpty(x.BusinessLicenseNumber));
    }

    private static bool BeValidMerchantType(string type)
    {
        var validTypes = new[] { "Individual", "Company", "individual", "company" };
        return validTypes.Contains(type);
    }
}

/// <summary>
/// EN: Validator for UpdateMerchantCommand.
/// VI: Validator cho UpdateMerchantCommand.
/// </summary>
public class UpdateMerchantCommandValidator : AbstractValidator<UpdateMerchantCommand>
{
    public UpdateMerchantCommandValidator()
    {
        RuleFor(x => x.BusinessName)
            .MaximumLength(200).WithMessage("Business name cannot exceed 200 characters")
            .When(x => !string.IsNullOrEmpty(x.BusinessName));

        RuleFor(x => x.TaxId)
            .MaximumLength(20).WithMessage("Tax ID cannot exceed 20 characters")
            .When(x => !string.IsNullOrEmpty(x.TaxId));

        RuleFor(x => x.BankCode)
            .MaximumLength(10).WithMessage("Bank code cannot exceed 10 characters")
            .When(x => !string.IsNullOrEmpty(x.BankCode));

        RuleFor(x => x.BankAccountNumber)
            .MaximumLength(30).WithMessage("Bank account number cannot exceed 30 characters")
            .When(x => !string.IsNullOrEmpty(x.BankAccountNumber));

        RuleFor(x => x.BankAccountHolderName)
            .MaximumLength(100).WithMessage("Account holder name cannot exceed 100 characters")
            .When(x => !string.IsNullOrEmpty(x.BankAccountHolderName));
    }
}
