import 'dart:io'; class BotConfig { BotConfig({ required this.backendUrl, required this.shareDailyLimit, required this.apiKey, required this.botToken, required this.adminIds, }); final String backendUrl; final int shareDailyLimit; final String apiKey; final String botToken; final List adminIds; static const String defaultBackendUrl = 'https://api.mnemo-cards.online'; static const int defaultShareDailyLimit = 1; factory BotConfig.fromEnvironment({ Map? environment, }) { final env = environment ?? Platform.environment; // Backend URL: MNEMO_BACKEND_URL or BACKEND_URL or default final backendUrl = [ env['MNEMO_BACKEND_URL'], env['BACKEND_URL'], defaultBackendUrl, ].firstWhere( (value) => value != null && value.trim().isNotEmpty, )!; // Share daily limit: BOT_SHARE_DAILY_LIMIT or default final shareDailyLimitStr = env['BOT_SHARE_DAILY_LIMIT']; final shareDailyLimit = shareDailyLimitStr != null ? int.tryParse(shareDailyLimitStr) ?? defaultShareDailyLimit : defaultShareDailyLimit; // API Key: required final apiKey = env['TELEGRAM_BOT_API_KEY']; if (apiKey == null || apiKey.isEmpty) { throw Exception( 'TELEGRAM_BOT_API_KEY environment variable is required'); } // Bot Token: required final botToken = env['TELEGRAM_BOT_TOKEN']; if (botToken == null || botToken.isEmpty) { throw Exception('TELEGRAM_BOT_TOKEN environment variable is required'); } // Admin IDs: required, comma or space separated final adminIdsStr = env['ADMIN_IDS']; if (adminIdsStr == null || adminIdsStr.trim().isEmpty) { throw Exception( 'ADMIN_IDS environment variable is required'); } final adminIds = adminIdsStr .split(RegExp(r'[,;\s]+')) .map((e) => e.trim()) .where((e) => e.isNotEmpty) .toList(); if (adminIds.isEmpty) { throw Exception( 'ADMIN_IDS must contain at least one admin ID'); } return BotConfig( backendUrl: backendUrl.trim(), shareDailyLimit: shareDailyLimit, apiKey: apiKey, botToken: botToken, adminIds: adminIds, ); } }