// EN: Validators for Auth commands (external login, 2FA, logout).
// VI: Validators cho cac commands Auth (external login, 2FA, logout).

using FluentValidation;
using IamService.API.Application.Commands.Auth;

namespace IamService.API.Application.Validations;

/// <summary>
/// EN: Validator for ExternalLoginCommand.
/// VI: Validator cho ExternalLoginCommand.
/// </summary>
public class ExternalLoginCommandValidator : AbstractValidator<ExternalLoginCommand>
{
    private static readonly string[] ValidProviders = ["Google", "Facebook", "Apple", "google", "facebook", "apple"];

    public ExternalLoginCommandValidator()
    {
        RuleFor(x => x.Provider)
            .NotEmpty()
            .WithMessage("Provider is required / Nha cung cap la bat buoc")
            .MaximumLength(50)
            .WithMessage("Provider must not exceed 50 characters / Nha cung cap khong vuot qua 50 ky tu")
            .Must(p => ValidProviders.Contains(p))
            .WithMessage("Invalid provider. Valid values: Google, Facebook, Apple / Nha cung cap khong hop le");

        RuleFor(x => x.ProviderUserId)
            .NotEmpty()
            .WithMessage("Provider user ID is required / ID nguoi dung tu nha cung cap la bat buoc")
            .MaximumLength(256)
            .WithMessage("Provider user ID must not exceed 256 characters / ID nguoi dung khong vuot qua 256 ky tu");

        RuleFor(x => x.Email)
            .NotEmpty()
            .WithMessage("Email is required / Email la bat buoc")
            .EmailAddress()
            .WithMessage("Invalid email format / Dinh dang email khong hop le")
            .MaximumLength(256)
            .WithMessage("Email must not exceed 256 characters / Email khong vuot qua 256 ky tu");

        RuleFor(x => x.Name)
            .MaximumLength(200)
            .WithMessage("Name must not exceed 200 characters / Ten khong vuot qua 200 ky tu")
            .When(x => x.Name != null);

        RuleFor(x => x.PictureUrl)
            .MaximumLength(2048)
            .WithMessage("Picture URL must not exceed 2048 characters / URL hinh anh khong vuot qua 2048 ky tu")
            .Must(url => url == null || Uri.TryCreate(url, UriKind.Absolute, out _))
            .WithMessage("Picture URL must be a valid URL / URL hinh anh phai hop le")
            .When(x => x.PictureUrl != null);
    }
}

/// <summary>
/// EN: Validator for Enable2FACommand.
/// VI: Validator cho Enable2FACommand.
/// </summary>
public class Enable2FACommandValidator : AbstractValidator<Enable2FACommand>
{
    public Enable2FACommandValidator()
    {
        RuleFor(x => x.UserId)
            .NotEmpty()
            .WithMessage("User ID is required / User ID la bat buoc");
    }
}

/// <summary>
/// EN: Validator for Verify2FACommand.
/// VI: Validator cho Verify2FACommand.
/// </summary>
public class Verify2FACommandValidator : AbstractValidator<Verify2FACommand>
{
    public Verify2FACommandValidator()
    {
        RuleFor(x => x.UserId)
            .NotEmpty()
            .WithMessage("User ID is required / User ID la bat buoc");

        RuleFor(x => x.Code)
            .NotEmpty()
            .WithMessage("2FA code is required / Ma 2FA la bat buoc")
            .MaximumLength(10)
            .WithMessage("2FA code must not exceed 10 characters / Ma 2FA khong vuot qua 10 ky tu")
            .Matches(@"^\d{6}$")
            .WithMessage("2FA code must be 6 digits / Ma 2FA phai la 6 chu so");
    }
}

/// <summary>
/// EN: Validator for Disable2FACommand.
/// VI: Validator cho Disable2FACommand.
/// </summary>
public class Disable2FACommandValidator : AbstractValidator<Disable2FACommand>
{
    public Disable2FACommandValidator()
    {
        RuleFor(x => x.UserId)
            .NotEmpty()
            .WithMessage("User ID is required / User ID la bat buoc");

        RuleFor(x => x.Code)
            .NotEmpty()
            .WithMessage("2FA code is required / Ma 2FA la bat buoc")
            .MaximumLength(10)
            .WithMessage("2FA code must not exceed 10 characters / Ma 2FA khong vuot qua 10 ky tu")
            .Matches(@"^\d{6}$")
            .WithMessage("2FA code must be 6 digits / Ma 2FA phai la 6 chu so");
    }
}

/// <summary>
/// EN: Validator for LogoutCommand.
/// VI: Validator cho LogoutCommand.
/// </summary>
public class LogoutCommandValidator : AbstractValidator<LogoutCommand>
{
    public LogoutCommandValidator()
    {
        RuleFor(x => x.UserId)
            .NotEmpty()
            .WithMessage("User ID is required / User ID la bat buoc");
    }
}

/// <summary>
/// EN: Validator for SendVerificationEmailCommand.
/// VI: Validator cho SendVerificationEmailCommand.
/// </summary>
public class SendVerificationEmailCommandValidator : AbstractValidator<SendVerificationEmailCommand>
{
    public SendVerificationEmailCommandValidator()
    {
        RuleFor(x => x.Email)
            .NotEmpty()
            .WithMessage("Email is required / Email la bat buoc")
            .EmailAddress()
            .WithMessage("Invalid email format / Dinh dang email khong hop le")
            .MaximumLength(256)
            .WithMessage("Email must not exceed 256 characters / Email khong vuot qua 256 ky tu");
    }
}

/// <summary>
/// EN: Validator for ConfirmEmailCommand.
/// VI: Validator cho ConfirmEmailCommand.
/// </summary>
public class ConfirmEmailCommandValidator : AbstractValidator<ConfirmEmailCommand>
{
    public ConfirmEmailCommandValidator()
    {
        RuleFor(x => x.Email)
            .NotEmpty()
            .WithMessage("Email is required / Email la bat buoc")
            .EmailAddress()
            .WithMessage("Invalid email format / Dinh dang email khong hop le")
            .MaximumLength(256)
            .WithMessage("Email must not exceed 256 characters / Email khong vuot qua 256 ky tu");

        RuleFor(x => x.Token)
            .NotEmpty()
            .WithMessage("Token is required / Token la bat buoc")
            .MaximumLength(1024)
            .WithMessage("Token must not exceed 1024 characters / Token khong vuot qua 1024 ky tu");
    }
}
