// Version: 1.0.0 | Created: 2026-04-01
// Typed failure hierarchy for authentication errors.
// UI maps these to human-readable messages — never expose raw exception text.

import 'package:freezed_annotation/freezed_annotation.dart';

part 'auth_failure.freezed.dart';

/// All ways authentication can fail.
@freezed
sealed class AuthFailure with _$AuthFailure {
  /// Username or password was incorrect.
  const factory AuthFailure.invalidCredentials() = InvalidCredentials;

  /// Could not reach the Matrix homeserver.
  const factory AuthFailure.networkError({String? message}) = NetworkError;

  /// The server returned an unexpected response.
  const factory AuthFailure.serverError({int? statusCode, String? message}) =
      ServerError;

  /// User's account has been disabled or deactivated.
  const factory AuthFailure.accountDisabled() = AccountDisabled;

  /// An unexpected error that doesn't fit the categories above.
  const factory AuthFailure.unknown({required String message}) = UnknownFailure;
}

/// Maps an [AuthFailure] to a user-facing string (Australian English).
extension AuthFailureMessage on AuthFailure {
  String get userMessage => switch (this) {
    InvalidCredentials() => 'Incorrect username or password. Please try again.',
    NetworkError() =>
      'Could not connect to the server. Check your internet connection and try again.',
    ServerError(statusCode: final code) =>
      'The server returned an error${code != null ? ' (code $code)' : ''}. Please try again shortly.',
    AccountDisabled() =>
      'Your account has been disabled. Please contact the administrator.',
    UnknownFailure(message: final msg) => 'Something went wrong: $msg',
  };
}
