/// Custom exception class to handle various Supabase-related errors.
class TSupabaseException implements Exception {
  /// The error code associated with the exception.
  final String code;

  /// Constructor that takes an error code.
  TSupabaseException(this.code);

  /// Get the corresponding error message based on the error code.
  String get message {
    switch (code) {
      // Auth errors
      case 'user-not-found':
        return 'No user found with the provided credentials.';
      case 'email-already-in-use':
      case 'email-already-exists':
        return 'The email address is already registered. Please use a different email.';
      case 'invalid-email':
        return 'The email address provided is invalid. Please enter a valid email.';
      case 'weak-password':
        return 'The password is too weak. Please choose a stronger password.';
      case 'user-disabled':
        return 'This user account has been disabled. Please contact support for assistance.';
      case 'wrong-password':
        return 'Incorrect password. Please check your password and try again.';
      case 'invalid-verification-code':
        return 'Invalid verification code. Please enter a valid code.';
      case 'invalid-verification-id':
        return 'Invalid verification ID. Please request a new verification code.';
      case 'expired-action-code':
        return 'The verification link has expired. Please request a new link.';
      case 'invalid-action-code':
        return 'The verification link is invalid. Please request a new link.';

      // Database errors
      case 'PGRST116':
        return 'Resource not found. The requested resource does not exist.';
      case 'PGRST104':
        return 'Invalid column specified in the query.';
      case 'PGRST105':
        return 'Invalid JSON format provided.';
      case 'PGRST108':
        return 'Malformed query string parameter.';
      case '23505':
        return 'Duplicate value violates unique constraint.';
      case '23503':
        return 'Foreign key violation. Related record not found.';
      case '42P01':
        return 'The requested table does not exist.';
      case '42703':
        return 'Column does not exist in the table.';

      // General API errors
      case 'rate-limit-exceeded':
        return 'Too many requests. Please try again later.';
      case 'server-error':
        return 'A server error occurred. Please try again later.';
      case 'unauthorized':
        return 'Unauthorized access. Please sign in again.';
      case 'forbidden':
        return 'Access denied. You do not have permission to perform this action.';
      case 'timeout':
        return 'The request timed out. Please check your internet connection.';

      // Storage errors
      case 'storage-object-not-found':
        return 'The requested file does not exist.';
      case 'storage-unauthorized':
        return 'You do not have permission to access this file.';

      // Network errors
      case 'network-request-failed':
        return 'Network request failed. Please check your internet connection.';

      // Auth specific errors from Supabase
      case 'auth/invalid-credential':
        return 'The supplied credential is invalid. Please check and try again.';
      case 'auth/user-not-found':
        return 'No user found with the provided email.';
      case 'auth/invalid-email':
        return 'The email address is invalid.';
      case 'auth/email-already-in-use':
        return 'The email address is already in use by another account.';
      case 'auth/operation-not-allowed':
        return 'This operation is not allowed.';
      case 'auth/requires-recent-login':
        return 'This operation requires re-authentication. Please log in again.';
      case 'auth/too-many-requests':
        return 'Too many unsuccessful login attempts. Please try again later.';
      case 'auth/invalid-api-key':
        return 'The API key is invalid. Please contact support.';
      case 'auth/user-token-expired':
        return 'Your session has expired. Please sign in again.';
      case 'auth/user-disabled':
        return 'Your account has been disabled. Please contact support.';
      case 'auth/invalid-password':
        return 'The password is invalid. Please try again.';

      default:
        return 'An unexpected error occurred. Please try again.';
    }
  }
}
