import 'package:flutter_dotenv/flutter_dotenv.dart'; /// 환경 설정 관리 클래스 class Environment { static const String dev = 'development'; static const String prod = 'production'; static late String _environment; /// 현재 환경 static String get current => _environment; /// 개발 환경 여부 static bool get isDevelopment => _environment == dev; /// 프로덕션 환경 여부 static bool get isProduction => _environment == prod; /// API 베이스 URL static String get apiBaseUrl { return dotenv.env['API_BASE_URL'] ?? 'https://superport.naturebridgeai.com/api/v1'; } /// API 타임아웃 (밀리초) static int get apiTimeout { final timeoutStr = dotenv.env['API_TIMEOUT'] ?? '30000'; return int.tryParse(timeoutStr) ?? 30000; } /// 로깅 활성화 여부 static bool get enableLogging { final loggingStr = dotenv.env['ENABLE_LOGGING'] ?? 'false'; return loggingStr.toLowerCase() == 'true'; } /// API 사용 여부 (false면 Mock 데이터 사용) static bool get useApi { final useApiStr = dotenv.env['USE_API']; print('[Environment] USE_API 원시값: $useApiStr'); if (useApiStr == null || useApiStr.isEmpty) { print('[Environment] USE_API가 설정되지 않음, 기본값 true 사용'); return true; } final result = useApiStr.toLowerCase() == 'true'; print('[Environment] USE_API 최종값: $result'); return result; } /// 환경 초기화 static Future initialize([String? environment]) async { _environment = environment ?? const String.fromEnvironment('ENVIRONMENT', defaultValue: dev); final envFile = _getEnvFile(); print('[Environment] 환경 초기화 중...'); print('[Environment] 현재 환경: $_environment'); print('[Environment] 환경 파일: $envFile'); try { await dotenv.load(fileName: envFile); print('[Environment] 환경 파일 로드 성공'); // 모든 환경 변수 출력 print('[Environment] 로드된 환경 변수:'); dotenv.env.forEach((key, value) { print('[Environment] $key: $value'); }); print('[Environment] --- 설정 값 확인 ---'); print('[Environment] API Base URL: ${dotenv.env['API_BASE_URL'] ?? '설정되지 않음'}'); print('[Environment] API Timeout: ${dotenv.env['API_TIMEOUT'] ?? '설정되지 않음'}'); print('[Environment] 로깅 활성화: ${dotenv.env['ENABLE_LOGGING'] ?? '설정되지 않음'}'); print('[Environment] API 사용 (원시값): ${dotenv.env['USE_API'] ?? '설정되지 않음'}'); print('[Environment] API 사용 (getter): $useApi'); } catch (e) { print('[Environment] ⚠️ 환경 파일 로드 실패: $envFile'); print('[Environment] 에러 상세: $e'); print('[Environment] 기본값을 사용합니다.'); // .env 파일이 없어도 계속 진행 } } /// 환경별 파일 경로 반환 static String _getEnvFile() { switch (_environment) { case prod: return '.env.production'; case dev: default: return '.env.development'; } } /// 환경 변수 가져오기 static String? get(String key) { return dotenv.env[key]; } /// 환경 변수 존재 여부 확인 static bool has(String key) { return dotenv.env.containsKey(key); } }