import '../l10n/strings.g.dart';

/// Translates error message keys to localized strings.
///
/// This utility function takes a dot-notation key like 'errors.auth.account_exists'
/// and returns the corresponding localized string from the translation system.
///
/// If the key is not found or doesn't match the expected format, the original
/// key is returned as a fallback.
///
/// Example:
/// ```dart
/// final message = translateErrorKey('errors.auth.account_exists');
/// // Returns: "An account with this email already exists. Try signing in instead."
/// ```
String translateErrorKey(String key) {
  final List<String> parts = key.split('.');

  // Expected format: errors.{category}.{error_type}
  if (parts.length != 3 || parts[0] != 'errors') {
    return key;
  }

  final String category = parts[1];
  final String errorType = parts[2];

  switch (category) {
    case 'auth':
      return _translateAuthError(errorType);
    case 'hub':
      return _translateHubError(errorType);
    case 'order':
      return _translateOrderError(errorType);
    case 'profile':
      return _translateProfileError(errorType);
    case 'shift':
      return _translateShiftError(errorType);
    case 'generic':
      return _translateGenericError(errorType);
    default:
      return key;
  }
}

String _translateAuthError(String errorType) {
  switch (errorType) {
    case 'invalid_credentials':
      return t.errors.auth.invalid_credentials;
    case 'account_exists':
      return t.errors.auth.account_exists;
    case 'session_expired':
      return t.errors.auth.session_expired;
    case 'user_not_found':
      return t.errors.auth.user_not_found;
    case 'unauthorized_app':
      return t.errors.auth.unauthorized_app;
    case 'weak_password':
      return t.errors.auth.weak_password;
    case 'sign_up_failed':
      return t.errors.auth.sign_up_failed;
    case 'sign_in_failed':
      return t.errors.auth.sign_in_failed;
    case 'not_authenticated':
      return t.errors.auth.not_authenticated;
    case 'password_mismatch':
      return t.errors.auth.password_mismatch;
    case 'google_only_account':
      return t.errors.auth.google_only_account;
    default:
      return t.errors.generic.unknown;
  }
}

String _translateHubError(String errorType) {
  switch (errorType) {
    case 'has_orders':
      return t.errors.hub.has_orders;
    case 'not_found':
      return t.errors.hub.not_found;
    case 'creation_failed':
      return t.errors.hub.creation_failed;
    default:
      return t.errors.generic.unknown;
  }
}

String _translateOrderError(String errorType) {
  switch (errorType) {
    case 'missing_hub':
      return t.errors.order.missing_hub;
    case 'missing_vendor':
      return t.errors.order.missing_vendor;
    case 'creation_failed':
      return t.errors.order.creation_failed;
    case 'shift_creation_failed':
      return t.errors.order.shift_creation_failed;
    case 'missing_business':
      return t.errors.order.missing_business;
    default:
      return t.errors.generic.unknown;
  }
}

String _translateProfileError(String errorType) {
  switch (errorType) {
    case 'staff_not_found':
      return t.errors.profile.staff_not_found;
    case 'business_not_found':
      return t.errors.profile.business_not_found;
    case 'update_failed':
      return t.errors.profile.update_failed;
    default:
      return t.errors.generic.unknown;
  }
}

String _translateShiftError(String errorType) {
  switch (errorType) {
    case 'no_open_roles':
      return t.errors.shift.no_open_roles;
    case 'application_not_found':
      return t.errors.shift.application_not_found;
    case 'no_active_shift':
      return t.errors.shift.no_active_shift;
    default:
      return t.errors.generic.unknown;
  }
}

String _translateGenericError(String errorType) {
  switch (errorType) {
    case 'unknown':
      return t.errors.generic.unknown;
    case 'no_connection':
      return t.errors.generic.no_connection;
    default:
      return t.errors.generic.unknown;
  }
}
