// lib/services/telegram_service.dart

import 'package:flutter/foundation.dart';
import 'package:sqflite/sqflite.dart';
import 'package:environment_monitoring_app/services/api_service.dart';
import 'package:environment_monitoring_app/services/database_helper.dart';

import 'package:environment_monitoring_app/services/settings_service.dart';

class TelegramService {
  ApiService? _apiService;
  final DatabaseHelper _dbHelper = DatabaseHelper();
  // REMOVED: The SettingsService is no longer needed here as we will perform a direct lookup.
  // final SettingsService _settingsService = SettingsService();

  bool _isProcessing = false;

  TelegramService({ApiService? apiService}) : _apiService = apiService;

  void setApiService(ApiService apiService) {
    _apiService = apiService;
  }

  // FIX: Replaced the brittle switch statement with a robust, generic lookup function.
  // This function can now find the Chat ID for ANY module, including 'river_in_situ'.
  String _getChatIdForModule(String module, List<Map<String, dynamic>>? appSettings) {
    if (appSettings == null) {
      return '';
    }
    try {
      final setting = appSettings.firstWhere(
              (settingMap) =>
          settingMap['module_name'] == module &&
              settingMap['setting_key'] == 'telegram_chat_id'
      );
      return setting['setting_value'] as String? ?? '';
    } catch (e) {
      // This catch block handles cases where no matching setting is found in the list.
      return '';
    }
  }

  /// Tries to send an alert immediately over the network.
  /// Returns `true` on success, `false` on failure.
  Future<bool> sendAlertImmediately(String module, String message, List<Map<String, dynamic>>? appSettings) async {
    debugPrint("[TelegramService] Attempting to send alert immediately for module: $module");
    String chatId = _getChatIdForModule(module, appSettings);

    if (chatId.isEmpty) {
      debugPrint("[TelegramService] ❌ Cannot send immediately. Chat ID for module '$module' is not configured.");
      return false;
    }

    if (_apiService == null) {
      debugPrint("[TelegramService] ❌ ApiService is not available.");
      return false;
    }

    final result = await _apiService!.sendTelegramAlert(
      chatId: chatId,
      message: message,
    );

    if (result['success'] == true) {
      debugPrint("[TelegramService] ✅ Alert sent immediately.");
      return true;
    } else {
      debugPrint("[TelegramService] ❌ Immediate send failed. Reason: ${result['message']}");
      return false;
    }
  }

  /// Saves an alert to the local database queue. (This is now the fallback)
  Future<void> queueMessage(String module, String message, List<Map<String, dynamic>>? appSettings) async {
    String chatId = _getChatIdForModule(module, appSettings);

    if (chatId.isEmpty) {
      debugPrint("[TelegramService] ❌ ERROR: Cannot queue alert. Chat ID for module '$module' is not configured.");
      return;
    }

    debugPrint("[TelegramService] ⬇️ Immediate send failed. Saving alert to local queue.");
    final db = await _dbHelper.database;
    await db.insert(
      'alert_queue',
      {
        'chat_id': chatId,
        'message': message,
        'created_at': DateTime.now().toIso8601String(),
      },
    );
    debugPrint("[TelegramService] ✅ Alert queued for module: $module");
  }

  /// Processes all pending alerts in the queue.
  Future<void> processAlertQueue() async {
    if (_isProcessing) {
      debugPrint("[TelegramService] ⏳ Queue is already being processed. Skipping.");
      return;
    }

    _isProcessing = true;
    debugPrint("[TelegramService] ▶️ Starting to process alert queue...");

    final db = await _dbHelper.database;
    final List<Map<String, dynamic>> pendingAlerts = await db.query('alert_queue', orderBy: 'created_at');

    if (pendingAlerts.isEmpty) {
      debugPrint("[TelegramService] ⏹️ Queue is empty. Nothing to process.");
      _isProcessing = false;
      return;
    }

    if (_apiService == null) {
      debugPrint("[TelegramService] ❌ ApiService is not available for processing queue.");
      _isProcessing = false;
      return;
    }

    debugPrint("[TelegramService] 🔎 Found ${pendingAlerts.length} pending alerts.");

    for (var alert in pendingAlerts) {
      final alertId = alert['id'];
      final chatId = alert['chat_id'];
      debugPrint("[TelegramService] - Processing alert ID: $alertId for Chat ID: $chatId");

      final result = await _apiService!.sendTelegramAlert(
        chatId: chatId,
        message: alert['message'],
      );

      if (result['success'] == true) {
        await db.delete('alert_queue', where: 'id = ?', whereArgs: [alertId]);
        debugPrint("[TelegramService]   ✅ SUCCESS: Alert ID $alertId sent and removed from queue.");
      } else {
        debugPrint("[TelegramService]   ❌ FAILED: Alert ID $alertId could not be sent. Reason: ${result['message']}");
      }
    }

    debugPrint("[TelegramService] ⏹️ Finished processing alert queue.");
    _isProcessing = false;
  }
}