// lib/screens/settings/telegram_alert_settings.dart

import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import 'package:environment_monitoring_app/auth_provider.dart';
import 'package:environment_monitoring_app/services/settings_service.dart';

class TelegramAlertSettingsScreen extends StatelessWidget {
  TelegramAlertSettingsScreen({super.key});

  // Helper service for parsing settings
  final SettingsService _settingsService = SettingsService();

  Widget _buildChatIdEntry(String label, String value) {
    return ListTile(
      contentPadding: EdgeInsets.zero,
      leading: const Icon(Icons.telegram, size: 20),
      title: Text('$label Chat ID'),
      subtitle: Text(value.isNotEmpty ? value : 'Not Set'),
      dense: true,
    );
  }

  Widget _buildSectionHeader(BuildContext context, String title) {
    return Padding(
      padding: const EdgeInsets.fromLTRB(8.0, 24.0, 8.0, 16.0),
      child: Text(
        title,
        style: Theme.of(context)
            .textTheme
            .headlineSmall
            ?.copyWith(fontWeight: FontWeight.bold),
      ),
    );
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text("Telegram Alert Settings"),
      ),
      body: Consumer<AuthProvider>(
        builder: (context, auth, child) {
          final appSettings = auth.appSettings;
          return ListView(
            padding: const EdgeInsets.all(16.0),
            children: [
              _buildSectionHeader(context, "Telegram Alerts"),
              Card(
                margin: EdgeInsets.zero,
                child: Padding(
                  padding: const EdgeInsets.all(16.0),
                  child: Column(
                    children: [
                      // --- MARINE SECTION ---
                      ExpansionTile(
                        title: const Text('Marine Alerts',
                            style: TextStyle(fontWeight: FontWeight.bold)),
                        initiallyExpanded: true,
                        children: [
                          _buildChatIdEntry('In-Situ',
                              _settingsService.getInSituChatId(appSettings)),
                          _buildChatIdEntry('Tarball',
                              _settingsService.getTarballChatId(appSettings)),
                          _buildChatIdEntry(
                              'Investigative',
                              _settingsService
                                  .getMarineInvestigativeChatId(appSettings)),
                          // --- ADDED: Marine Report ---
                          _buildChatIdEntry(
                              'NPE Report',
                              _settingsService.getMarineReportChatId(appSettings)),
                        ],
                      ),

                      // --- RIVER SECTION ---
                      ExpansionTile(
                        title: const Text('River Alerts',
                            style: TextStyle(fontWeight: FontWeight.bold)),
                        initiallyExpanded: false,
                        children: [
                          _buildChatIdEntry('In-Situ',
                              _settingsService.getRiverInSituChatId(appSettings)),
                          _buildChatIdEntry(
                              'Triennial',
                              _settingsService
                                  .getRiverTriennialChatId(appSettings)),
                          _buildChatIdEntry(
                              'Investigative',
                              _settingsService
                                  .getRiverInvestigativeChatId(appSettings)),
                          // --- ADDED: River Report ---
                          _buildChatIdEntry(
                              'NPE Report',
                              _settingsService.getRiverReportChatId(appSettings)),
                        ],
                      ),

                      // --- AIR SECTION ---
                      ExpansionTile(
                        title: const Text('Air Alerts',
                            style: TextStyle(fontWeight: FontWeight.bold)),
                        initiallyExpanded: false,
                        children: [
                          _buildChatIdEntry('Manual',
                              _settingsService.getAirManualChatId(appSettings)),
                          _buildChatIdEntry(
                              'Investigative',
                              _settingsService
                                  .getAirInvestigativeChatId(appSettings)),
                        ],
                      ),
                    ],
                  ),
                ),
              ),
            ],
          );
        },
      ),
    );
  }
}