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/settings_service.dart';

class TelegramService {
  final ApiService _apiService = ApiService();
  final DatabaseHelper _dbHelper = DatabaseHelper();
  final SettingsService _settingsService = SettingsService();

  bool _isProcessing = false;

  // --- ADDED: New method to attempt immediate sending ---
  /// Tries to send an alert immediately over the network.
  /// Returns `true` on success, `false` on failure.
  Future<bool> sendAlertImmediately(String module, String message) async {
    debugPrint("[TelegramService] Attempting to send alert immediately...");
    String chatId = '';
    if (module == 'marine_in_situ') {
      chatId = await _settingsService.getInSituChatId();
    } else if (module == 'marine_tarball') {
      chatId = await _settingsService.getTarballChatId();
    }

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

    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) async {
    String chatId = '';
    if (module == 'marine_in_situ') {
      chatId = await _settingsService.getInSituChatId();
    } else if (module == 'marine_tarball') {
      chatId = await _settingsService.getTarballChatId();
    }

    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. (Unchanged)
  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;
    }

    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;
  }
}