class TValidators {
  // Generic user input validator with customizable error messages
  static String? validateUserInput(
    String? fieldName,
    String? value,
    int? maxLength, {
    int minLength = 1,
    bool required = true,
    String? customEmptyMessage,
    String? customLengthMessage,
  }) {
    // Check for empty value
    if (required && (value == null || value.isEmpty)) {
      return customEmptyMessage ?? '$fieldName is required.';
    }

    // If not required and empty, no need for further validation
    if (!required && (value == null || value.isEmpty)) {
      return null;
    }

    // Check for minimum length
    if (minLength > 1 && value!.length < minLength) {
      return '$fieldName must be at least $minLength characters long.';
    }

    // Check for maximum length
    if (maxLength != null && value!.length > maxLength) {
      return customLengthMessage ??
          '$fieldName must be no more than $maxLength characters long.';
    }

    return null;
  }

  static String? validateEmptyText(
    String? fieldName,
    String? value, {
    String? customMessage,
  }) {
    if (value == null || value.isEmpty) {
      return customMessage ?? '$fieldName is required.';
    }

    return null;
  }

  static String? validateLength(
    String? value,
    int maxLength, {
    int minLength = 1,
    String? fieldName,
    String? customMessage,
  }) {
    if (value == null || value.isEmpty) {
      return '$fieldName is required.';
    }

    if (value.length < minLength) {
      return '${fieldName ?? 'Value'} must be at least $minLength characters long.';
    }

    if (value.length > maxLength) {
      return customMessage ??
          '${fieldName ?? 'Value'} must be no more than $maxLength characters long.';
    }

    return null;
  }

  static String? validateEmail(
    String? value, {
    bool required = true,
    String? customEmptyMessage,
    String? customInvalidMessage,
  }) {
    if (required && (value == null || value.isEmpty)) {
      return customEmptyMessage ?? 'Email is required.';
    }

    // If not required and empty, no need for further validation
    if (!required && (value == null || value.isEmpty)) {
      return null;
    }

    // Regular expression for email validation
    final emailRegExp = RegExp(r'^[\w-\.]+@([\w-]+\.)+[\w-]{2,4}$');

    if (!emailRegExp.hasMatch(value!)) {
      return customInvalidMessage ?? 'Invalid email address.';
    }

    return null;
  }

  static String? validatePassword(
    String? value, {
    bool requireUppercase = true,
    bool requireNumber = true,
    bool requireSpecialChar = false,
    int minLength = 6,
    String? customEmptyMessage,
    String? customLengthMessage,
    String? customUppercaseMessage,
    String? customNumberMessage,
    String? customSpecialCharMessage,
  }) {
    if (value == null || value.isEmpty) {
      return customEmptyMessage ?? 'Password is required.';
    }

    // Check for minimum password length
    if (value.length < minLength) {
      return customLengthMessage ??
          'Password must be at least $minLength characters long.';
    }

    // Check for uppercase letters
    if (requireUppercase && !value.contains(RegExp(r'[A-Z]'))) {
      return customUppercaseMessage ??
          'Password must contain at least one uppercase letter.';
    }

    // Check for numbers
    if (requireNumber && !value.contains(RegExp(r'[0-9]'))) {
      return customNumberMessage ??
          'Password must contain at least one number.';
    }

    // Check for special characters
    if (requireSpecialChar &&
        !value.contains(RegExp(r'[!@#$%^&*(),.?":{}|<>]'))) {
      return customSpecialCharMessage ??
          'Password must contain at least one special character.';
    }

    return null;
  }

  static String? validateConfirmPassword(
    String? password,
    String? confirmPassword, {
    String? customEmptyMessage,
    String? customMatchMessage,
  }) {
    if (confirmPassword == null || confirmPassword.isEmpty) {
      return customEmptyMessage ?? 'Confirm password is required.';
    }

    if (password != confirmPassword) {
      return customMatchMessage ?? 'Passwords do not match.';
    }

    return null;
  }

  static String? validatePhoneNumber(
    String? value, {
    bool required = true,
    String? customEmptyMessage,
    String? customInvalidMessage,
    String? customLengthMessage,
  }) {
    if (required && (value == null || value.isEmpty)) {
      return customEmptyMessage ?? 'Phone number is required.';
    }

    // If not required and empty, no need for further validation
    if (!required && (value == null || value.isEmpty)) {
      return null;
    }

    // Clean the number of spaces and special characters
    value = value!.replaceAll(RegExp(r'[\s\-()]'), '');

    // Regular expression for Indonesian phone number format
    final phoneRegExp = RegExp(r'^(?:(?:\+62|62)|0)8[1-9][0-9]{8,10}$');

    if (!phoneRegExp.hasMatch(value)) {
      return customInvalidMessage ??
          'Invalid phone number format. Example: 081234567890';
    }

    // Validate number length (10-13 digits)
    int length =
        value.startsWith('0')
            ? value.length
            : value.startsWith('62')
            ? value.length - 2
            : value.length - 3;

    if (length < 10 || length > 13) {
      return customLengthMessage ??
          'Phone number must be between 10-13 digits.';
    }

    return null;
  }
 
  static String? validateNRP(String? value) {
    return validateUserInput('NRP', value, 50, minLength: 5);
  }

  static String? validateRank(String? value) {
    return validateUserInput('Rank', value, 50);
  }

  static String? validatePosition(String? value) {
    return validateUserInput('Position', value, 100);
  }

  static String? validateUnitId(String? value) {
    if (value == null || value.isEmpty) {
      return 'Please select a unit.';
    }
    return null;
  }

  static String? validateRating(double? rating, {double minRating = 0}) {
    if (rating == null || rating <= minRating) {
      return 'Rating is required.';
    }
    return null;
  }
}
