/// Exception class for handling various errors.
class TExceptions implements Exception {
  /// The associated error message.
  final String message;

  /// Default constructor with a generic error message.
  const TExceptions([
    this.message = 'An unexpected error occurred. Please try again.',
  ]);

  /// Create an authentication exception from an error code.
  factory TExceptions.fromCode(String code) {
    switch (code) {
      // Common Auth Errors
      case 'email-already-in-use':
      case 'auth/email-already-in-use':
        return const TExceptions(
          'The email address is already registered. Please use a different email.',
        );
      case 'invalid-email':
      case 'auth/invalid-email':
        return const TExceptions(
          'The email address provided is invalid. Please enter a valid email.',
        );
      case 'weak-password':
      case 'auth/weak-password':
        return const TExceptions(
          'The password is too weak. Please choose a stronger password.',
        );
      case 'user-disabled':
      case 'auth/user-disabled':
        return const TExceptions(
          'This user account has been disabled. Please contact support for assistance.',
        );
      case 'user-not-found':
      case 'auth/user-not-found':
        return const TExceptions('Invalid login details. User not found.');
      case 'wrong-password':
      case 'auth/wrong-password':
        return const TExceptions(
          'Incorrect password. Please check your password and try again.',
        );
      case 'INVALID_LOGIN_CREDENTIALS':
      case 'auth/invalid-login-credentials':
        return const TExceptions(
          'Invalid login credentials. Please double-check your information.',
        );

      // Supabase Specific Errors
      case 'auth/redirect-cancelled-by-user':
        return const TExceptions('Sign-in was cancelled.');
      case 'auth/popup-closed-by-user':
        return const TExceptions('Sign-in popup was closed before completion.');
      case 'auth/provider-already-linked':
        return const TExceptions(
          'This account is already linked with another provider.',
        );
      case 'auth/requires-recent-login':
        return const TExceptions(
          'This operation requires recent authentication. Please log in again.',
        );

      // PostgreSQL Errors
      case 'PGRST116':
        return const TExceptions(
          'Resource not found. The requested resource does not exist.',
        );
      case '23505':
        return const TExceptions(
          'A record with the same value already exists.',
        );
      case '23503':
        return const TExceptions(
          'Related record not found. Reference constraint failed.',
        );

      // Common Network/Request Errors
      case 'too-many-requests':
      case 'auth/too-many-requests':
        return const TExceptions('Too many requests. Please try again later.');
      case 'network-request-failed':
        return const TExceptions(
          'Network request failed. Please check your internet connection.',
        );
      case 'internal-error':
      case 'auth/internal-error':
        return const TExceptions('Internal error. Please try again later.');
      case 'invalid-verification-code':
      case 'auth/invalid-verification-code':
        return const TExceptions(
          'Invalid verification code. Please enter a valid code.',
        );
      case 'invalid-verification-id':
      case 'auth/invalid-verification-id':
        return const TExceptions(
          'Invalid verification ID. Please request a new verification code.',
        );
      case 'quota-exceeded':
      case 'auth/quota-exceeded':
        return const TExceptions('Quota exceeded. Please try again later.');

      default:
        return const TExceptions();
    }
  }
}
