import 'local_config.dart';

/// Environnements de déploiement de l'application
enum Environment { dev, staging, prod }

/// Configuration centralisée par environnement.
/// Les URLs sont injectées via --dart-define=ENV=dev|staging|prod
/// L'IP dev est définie dans android/local.properties → dev.host
/// (propagée automatiquement vers ce fichier via build.gradle).
class AppConfig {
  static late final Environment _environment;
  static late final String apiBaseUrl;
  static late final String keycloakBaseUrl;
  static late final String wsBaseUrl;
  static late final bool enableDebugMode;
  static late final bool enableLogging;
  static late final bool enableCrashReporting;
  static late final bool enableAnalytics;
  static late final String sentryDsn;

  /// Initialise la configuration à partir de l'environnement.
  /// Appeler dans main() avant runApp().
  static void initialize() {
    const envString = String.fromEnvironment('ENV', defaultValue: 'dev');
    _environment = Environment.values.firstWhere(
      (e) => e.name == envString,
      orElse: () => Environment.dev,
    );

    switch (_environment) {
      case Environment.dev:
        apiBaseUrl = const String.fromEnvironment(
          'API_URL',
          defaultValue: 'http://$kDevHost:8085',
        );
        keycloakBaseUrl = const String.fromEnvironment(
          'KEYCLOAK_URL',
          defaultValue: 'http://$kDevHost:8180',
        );
        wsBaseUrl = const String.fromEnvironment(
          'WS_URL',
          defaultValue: 'ws://$kDevHost:8085',
        );
        enableDebugMode = true;
        enableLogging = true;
        enableCrashReporting = false;
        enableAnalytics = false;
        sentryDsn = '';

      case Environment.staging:
        apiBaseUrl = const String.fromEnvironment(
          'API_URL',
          defaultValue: 'https://api-staging.lions.dev',
        );
        keycloakBaseUrl = const String.fromEnvironment(
          'KEYCLOAK_URL',
          defaultValue: 'https://security-staging.lions.dev',
        );
        wsBaseUrl = const String.fromEnvironment(
          'WS_URL',
          defaultValue: 'wss://api-staging.lions.dev',
        );
        enableDebugMode = false;
        enableLogging = true;
        enableCrashReporting = true;
        enableAnalytics = false;
        sentryDsn = const String.fromEnvironment('SENTRY_DSN', defaultValue: '');

      case Environment.prod:
        apiBaseUrl = const String.fromEnvironment(
          'API_URL',
          defaultValue: 'https://api.lions.dev/unionflow',
        );
        keycloakBaseUrl = const String.fromEnvironment(
          'KEYCLOAK_URL',
          defaultValue: 'https://security.lions.dev',
        );
        wsBaseUrl = const String.fromEnvironment(
          'WS_URL',
          defaultValue: 'wss://api.lions.dev/unionflow',
        );
        enableDebugMode = false;
        enableLogging = false;
        enableCrashReporting = true;
        enableAnalytics = true;
        sentryDsn = const String.fromEnvironment('SENTRY_DSN', defaultValue: '');
    }
  }

  static Environment get environment => _environment;
  static bool get isDev => _environment == Environment.dev;
  static bool get isStaging => _environment == Environment.staging;
  static bool get isProd => _environment == Environment.prod;

  /// URL complète du realm Keycloak
  static String get keycloakRealmUrl => '$keycloakBaseUrl/realms/unionflow';

  /// URL du endpoint token Keycloak
  static String get keycloakTokenUrl =>
      '$keycloakRealmUrl/protocol/openid-connect/token';

  /// URL du endpoint WebSocket dashboard
  static String get wsDashboardUrl => '$wsBaseUrl/ws/dashboard';
}
