using FluentValidation;
using Meezi.API.Models.Crm;
using Meezi.Core.Utilities;

namespace Meezi.API.Validators;

public class CreateCustomerRequestValidator : AbstractValidator<CreateCustomerRequest>
{
    public CreateCustomerRequestValidator()
    {
        RuleFor(x => x.Name).NotEmpty().MaximumLength(200);
        RuleFor(x => x.Phone)
            .NotEmpty()
            .Must(p => PhoneNormalizer.IsValidIranMobile(PhoneNormalizer.Normalize(p)))
            .WithMessage("Invalid Iranian mobile number.");
        RuleFor(x => x.NationalId)
            .Matches(@"^\d{10}$")
            .When(x => !string.IsNullOrWhiteSpace(x.NationalId))
            .WithMessage("National ID must be 10 digits.");
        RuleFor(x => x.Group).IsInEnum();
    }
}

public class UpdateCustomerRequestValidator : AbstractValidator<UpdateCustomerRequest>
{
    public UpdateCustomerRequestValidator()
    {
        RuleFor(x => x.Name)
            .NotEmpty()
            .MaximumLength(200)
            .When(x => x.Name is not null);
        RuleFor(x => x.Phone)
            .Must(p => PhoneNormalizer.IsValidIranMobile(PhoneNormalizer.Normalize(p!)))
            .When(x => !string.IsNullOrWhiteSpace(x.Phone))
            .WithMessage("Invalid Iranian mobile number.");
        RuleFor(x => x.NationalId)
            .Matches(@"^\d{10}$")
            .When(x => !string.IsNullOrWhiteSpace(x.NationalId))
            .WithMessage("National ID must be 10 digits.");
        RuleFor(x => x.Group)
            .IsInEnum()
            .When(x => x.Group.HasValue);
        RuleFor(x => x.LoyaltyPoints)
            .GreaterThanOrEqualTo(0)
            .When(x => x.LoyaltyPoints.HasValue);
    }
}

public class CreateCouponRequestValidator : AbstractValidator<CreateCouponRequest>
{
    public CreateCouponRequestValidator()
    {
        RuleFor(x => x.Code).NotEmpty().MaximumLength(50);
        RuleFor(x => x.Type).IsInEnum();
        RuleFor(x => x.Value).GreaterThan(0);
    }
}

public class SendSmsCampaignRequestValidator : AbstractValidator<SendSmsCampaignRequest>
{
    public SendSmsCampaignRequestValidator()
    {
        RuleFor(x => x.Message).NotEmpty().MaximumLength(500);
        RuleFor(x => x)
            .Must(x => x.TargetGroup.HasValue || (x.Phones is { Count: > 0 }))
            .WithMessage("Specify TargetGroup or at least one phone number.");
    }
}
