// lib/services/settings_service.dart

import 'package:flutter/foundation.dart';

class SettingsService {
  // The service no longer manages its own state or makes API calls.
  // It is now a utility for parsing the settings list managed by AuthProvider.

  /// A private helper method to find a specific setting value from the cached list.
  String _getChatId(List<Map<String, dynamic>>? settings, String moduleName) {
    if (settings == null) {
      debugPrint("SettingsService: Cannot get Chat ID for '$moduleName', settings list is null.");
      return '';
    }

    try {
      // Find the specific setting map where the module_name and setting_key match.
      final setting = settings.firstWhere(
            (s) => s['module_name'] == moduleName && s['setting_key'] == 'telegram_chat_id',
        // If no matching setting is found, return an empty map to avoid errors.
        orElse: () => {},
      );

      if (setting.isNotEmpty) {
        return setting['setting_value']?.toString() ?? '';
      }
    } catch (e) {
      debugPrint("SettingsService: Error parsing chat ID for '$moduleName': $e");
    }

    debugPrint("SettingsService: Chat ID for module '$moduleName' not found.");
    return '';
  }

  /// Gets the Chat ID for the Marine In-Situ module.
  String getInSituChatId(List<Map<String, dynamic>>? settings) {
    return _getChatId(settings, 'marine_in_situ');
  }

  /// Gets the Chat ID for the Tarball module.
  String getTarballChatId(List<Map<String, dynamic>>? settings) {
    return _getChatId(settings, 'marine_tarball');
  }

  /// Gets the Chat ID for the Marine Investigative module.
  String getMarineInvestigativeChatId(List<Map<String, dynamic>>? settings) {
    return _getChatId(settings, 'marine_investigative');
  }

  // --- ADDED: Getter for Marine NPE Report ---
  String getMarineReportChatId(List<Map<String, dynamic>>? settings) {
    return _getChatId(settings, 'marine_report');
  }
  // ------------------------------------------

  /// Gets the Chat ID for the River In-Situ module.
  String getRiverInSituChatId(List<Map<String, dynamic>>? settings) {
    return _getChatId(settings, 'river_in_situ');
  }

  /// Gets the Chat ID for the River Triennial module.
  String getRiverTriennialChatId(List<Map<String, dynamic>>? settings) {
    return _getChatId(settings, 'river_triennial');
  }

  /// Gets the Chat ID for the River Investigative module.
  String getRiverInvestigativeChatId(List<Map<String, dynamic>>? settings) {
    return _getChatId(settings, 'river_investigative');
  }

  // --- ADDED: Getter for River NPE Report ---
  String getRiverReportChatId(List<Map<String, dynamic>>? settings) {
    return _getChatId(settings, 'river_report');
  }
  // ------------------------------------------

  /// Gets the Chat ID for the Air Manual module.
  String getAirManualChatId(List<Map<String, dynamic>>? settings) {
    return _getChatId(settings, 'air_manual');
  }

  /// Gets the Chat ID for the Air Investigative module.
  String getAirInvestigativeChatId(List<Map<String, dynamic>>? settings) {
    return _getChatId(settings, 'air_investigative');
  }
}