using FluentValidation;
using Meezi.API.Models.Auth;
using Meezi.Core.Utilities;
using System.Text.RegularExpressions;

namespace Meezi.API.Validators;

public class SendOtpRequestValidator : AbstractValidator<SendOtpRequest>
{
    public SendOtpRequestValidator()
    {
        RuleFor(x => x.Phone)
            .NotEmpty()
            .Must(p => PhoneNormalizer.IsValidIranMobile(PhoneNormalizer.Normalize(p)))
            .WithMessage("Invalid Iranian mobile number.");
    }
}

public class VerifyOtpRequestValidator : AbstractValidator<VerifyOtpRequest>
{
    public VerifyOtpRequestValidator()
    {
        RuleFor(x => x.Phone)
            .NotEmpty()
            .Must(p => PhoneNormalizer.IsValidIranMobile(PhoneNormalizer.Normalize(p)))
            .WithMessage("Invalid Iranian mobile number.");

        RuleFor(x => x.Code)
            .Must(OtpNormalizer.IsValidSixDigitCode)
            .WithMessage("OTP must be 6 digits.");
    }
}

public class RefreshTokenRequestValidator : AbstractValidator<RefreshTokenRequest>
{
    public RefreshTokenRequestValidator()
    {
        RuleFor(x => x.RefreshToken).NotEmpty();
    }
}

public class RegisterRequestValidator : AbstractValidator<RegisterRequest>
{
    public RegisterRequestValidator()
    {
        RuleFor(x => x.Phone)
            .NotEmpty()
            .Must(p => PhoneNormalizer.IsValidIranMobile(PhoneNormalizer.Normalize(p)))
            .WithMessage("Invalid Iranian mobile number.");

        RuleFor(x => x.CafeName)
            .NotEmpty()
            .MaximumLength(100)
            .WithMessage("Cafe name must be between 1 and 100 characters.");

        RuleFor(x => x.Slug)
            .Must(s => s == null || SlugHelper.IsValidSlug(s))
            .WithMessage("Slug must be 2-80 lowercase letters, digits, or hyphens (e.g. my-cafe).");
    }
}

public class VerifyRegisterRequestValidator : AbstractValidator<VerifyRegisterRequest>
{
    public VerifyRegisterRequestValidator()
    {
        RuleFor(x => x.Phone)
            .NotEmpty()
            .Must(p => PhoneNormalizer.IsValidIranMobile(PhoneNormalizer.Normalize(p)))
            .WithMessage("Invalid Iranian mobile number.");

        RuleFor(x => x.Code)
            .Must(OtpNormalizer.IsValidSixDigitCode)
            .WithMessage("OTP must be 6 digits.");
    }
}
