import 'env_debug.dart';
import 'env_release.dart';

/// 应用配置
/// 接口地址按 debug/release 自动切换，见 env_debug.dart / env_release.dart
/// 也可通过 --dart-define 覆盖
abstract class AppConfig {
  /// 是否为 Debug 构建
  static const bool isDebug = !bool.fromEnvironment('dart.vm.product');

  // ── dart-define 覆盖入口（为空则走 env 文件默认值）────
  static const String _overrideApiBaseUrl =
      String.fromEnvironment('API_BASE_URL', defaultValue: '');
  static const String _overrideWsUrl =
      String.fromEnvironment('WS_URL', defaultValue: '');
  static const String _overrideWsSpotUrl =
      String.fromEnvironment('WS_SPOT_URL', defaultValue: '');

  static const String _overrideWalletConnectProjectId =
      String.fromEnvironment('WALLETCONNECT_PROJECT_ID', defaultValue: '');

  static const String _overrideTronFullHost =
      String.fromEnvironment('TRON_FULL_HOST', defaultValue: '');
  static const String _overrideTronGridApiKey =
      String.fromEnvironment('TRONGRID_API_KEY', defaultValue: '');

  /// 实际使用的 API 地址
  static String get effectiveApiBaseUrl => _overrideApiBaseUrl.isNotEmpty
      ? _overrideApiBaseUrl
      : (isDebug ? EnvDebug.apiBaseUrl : EnvRelease.apiBaseUrl);

  /// 实际使用的 WS 地址
  static String get effectiveWsUrl => _overrideWsUrl.isNotEmpty
      ? _overrideWsUrl
      : (isDebug ? EnvDebug.wsUrl : EnvRelease.wsUrl);

  /// 实际使用的现货 WS 地址
  static String get effectiveWsSpotUrl => _overrideWsSpotUrl.isNotEmpty
      ? _overrideWsSpotUrl
      : (isDebug ? EnvDebug.wsSpotUrl : EnvRelease.wsSpotUrl);

  /// 兜底 Host 列表
  static List<String> get fallbackHosts =>
      isDebug ? EnvDebug.fallbackHosts : EnvRelease.fallbackHosts;

  /// 远程配置 JSON 地址列表（按顺序尝试，任一成功即止；仅 release 生效）
  static List<String> get remoteConfigUrls =>
      isDebug ? EnvDebug.remoteConfigUrls : EnvRelease.remoteConfigUrls;

  /// 是否显示客服入口（首页、设置、带单申请等）
  static bool get customerServiceEnabled =>
      isDebug ? EnvDebug.customerServiceEnabled : EnvRelease.customerServiceEnabled;

  /// 客服服务地址
  static String get customerServiceHost =>
      isDebug ? EnvDebug.customerServiceHost : EnvRelease.customerServiceHost;

  /// 客服聊天页面路径
  static String get customerServiceChatPath =>
      isDebug ? EnvDebug.customerServiceChatPath : EnvRelease.customerServiceChatPath;

  /// Tron FullNode（与 Web `VITE_TRON_FULL_HOST`，默认 api.trongrid.io）
  static String get tronFullHost {
    final o = _overrideTronFullHost.trim();
    if (o.isNotEmpty) {
      return o;
    }
    final e =
        isDebug ? EnvDebug.tronFullHost.trim() : EnvRelease.tronFullHost.trim();
    if (e.isNotEmpty) {
      return e;
    }
    return 'https://api.trongrid.io';
  }

  /// TronGrid API Key（可选，与 Web `VITE_TRONGRID_API_KEY`）
  static String get tronGridApiKey {
    final o = _overrideTronGridApiKey.trim();
    if (o.isNotEmpty) {
      return o;
    }
    return isDebug
        ? EnvDebug.tronGridApiKey.trim()
        : EnvRelease.tronGridApiKey.trim();
  }

  /// Reown / WalletConnect Project ID（与 Web 端 `VITE_WALLETCONNECT_PROJECT_ID` 一致；务经 CI / 本地 dart-define 注入）
  static String get walletConnectProjectId {
    final o = _overrideWalletConnectProjectId.trim();
    if (o.isNotEmpty) {
      return o;
    }
    return isDebug
        ? EnvDebug.walletConnectProjectId.trim()
        : EnvRelease.walletConnectProjectId.trim();
  }

  /// CDN 地址（图片资源）
  static const String cdnUrl = String.fromEnvironment(
    'CDN_URL',
    defaultValue: '',
  );

  /// 应用版本号（与 pubspec.yaml version 保持一致）
  static const String appVersion = '4.1.5';

  /// 是否启用 Mock 模式
  static const bool enableMock = bool.fromEnvironment(
    'ENABLE_MOCK',
    defaultValue: false,
  );

  /// 连接超时（毫秒）
  static const int connectTimeoutMs = 15000;

  /// 接收超时（毫秒）
  static const int receiveTimeoutMs = 15000;

  /// 测速超时（毫秒）
  static const int speedTestTimeoutMs = 3000;

  /// 节点故障转移连续失败阈值
  static const int failoverThreshold = 3;

  /// 熔断恢复时间（秒）
  static const int circuitBreakDurationSec = 60;
}
