import 'package:flutter/material.dart';
import 'package:em2rp/models/alert_model.dart';
import 'package:em2rp/services/alert_service.dart';
import 'package:em2rp/providers/local_user_provider.dart';
import 'package:em2rp/views/widgets/alert_item.dart';
import 'package:em2rp/utils/colors.dart';
import 'package:provider/provider.dart';

/// Page listant toutes les alertes de l'utilisateur
class AlertsPage extends StatefulWidget {
  const AlertsPage({super.key});

  @override
  State<AlertsPage> createState() => _AlertsPageState();
}

class _AlertsPageState extends State<AlertsPage> with SingleTickerProviderStateMixin {
  late TabController _tabController;
  final AlertService _alertService = AlertService();
  AlertType? _filter;

  @override
  void initState() {
    super.initState();
    _tabController = TabController(length: 4, vsync: this);
    _tabController.addListener(() {
      setState(() {
        _filter = _getFilterForTab(_tabController.index);
      });
    });
  }

  @override
  void dispose() {
    _tabController.dispose();
    super.dispose();
  }

  AlertType? _getFilterForTab(int index) {
    switch (index) {
      case 0:
        return null; // Toutes
      case 1:
        return AlertType.eventCreated; // Événements (on filtrera manuellement)
      case 2:
        return AlertType.maintenanceDue; // Maintenance
      case 3:
        return AlertType.lost; // Équipement
      default:
        return null;
    }
  }

  @override
  Widget build(BuildContext context) {
    final localUserProvider = context.watch<LocalUserProvider>();
    final userId = localUserProvider.currentUser?.uid;

    if (userId == null) {
      return Scaffold(
        appBar: AppBar(
          title: const Text('Notifications'),
        ),
        body: const Center(
          child: Text('Veuillez vous connecter'),
        ),
      );
    }

    return Scaffold(
      appBar: AppBar(
        title: const Text('Notifications'),
        backgroundColor: AppColors.rouge,
        actions: [
          IconButton(
            icon: const Icon(Icons.done_all),
            onPressed: () => _markAllAsRead(userId),
            tooltip: 'Tout marquer comme lu',
          ),
        ],
        bottom: TabBar(
          controller: _tabController,
          indicatorColor: Colors.white,
          labelColor: Colors.white,
          unselectedLabelColor: Colors.white70,
          tabs: const [
            Tab(text: 'Toutes'),
            Tab(text: 'Événements'),
            Tab(text: 'Maintenance'),
            Tab(text: 'Équipement'),
          ],
        ),
      ),
      body: _buildAlertsList(userId),
    );
  }

  Widget _buildAlertsList(String userId) {
    return StreamBuilder<List<AlertModel>>(
      stream: _alertService.alertsStreamForUser(userId),
      builder: (context, snapshot) {
        if (snapshot.connectionState == ConnectionState.waiting) {
          return const Center(child: CircularProgressIndicator());
        }

        if (snapshot.hasError) {
          // Log détaillé de l'erreur
          print('[AlertsPage] ERREUR Stream: ${snapshot.error}');
          print('[AlertsPage] StackTrace: ${snapshot.stackTrace}');

          return Center(
            child: Column(
              mainAxisAlignment: MainAxisAlignment.center,
              children: [
                Icon(Icons.error_outline, size: 64, color: Colors.red),
                const SizedBox(height: 16),
                Text('Erreur de chargement des alertes'),
                const SizedBox(height: 8),
                Text(
                  snapshot.error.toString(),
                  style: TextStyle(fontSize: 12, color: Colors.grey),
                  textAlign: TextAlign.center,
                ),
                const SizedBox(height: 8),
                ElevatedButton(
                  onPressed: () => setState(() {}),
                  child: const Text('Réessayer'),
                ),
              ],
            ),
          );
        }

        final allAlerts = snapshot.data ?? [];

        // Filtrer selon l'onglet sélectionné
        final filteredAlerts = _filterAlerts(allAlerts);

        if (filteredAlerts.isEmpty) {
          return _buildEmptyState();
        }

        return RefreshIndicator(
          onRefresh: () async {
            setState(() {});
          },
          child: ListView.builder(
            padding: const EdgeInsets.symmetric(vertical: 8),
            itemCount: filteredAlerts.length,
            itemBuilder: (context, index) {
              final alert = filteredAlerts[index];
              return AlertItem(
                alert: alert,
                onTap: () => _handleAlertTap(alert),
                onMarkAsRead: () => _markAsRead(alert.id),
                onDelete: () => _deleteAlert(alert.id),
              );
            },
          ),
        );
      },
    );
  }

  List<AlertModel> _filterAlerts(List<AlertModel> alerts) {
    if (_filter == null) {
      return alerts; // Toutes
    }

    switch (_tabController.index) {
      case 1: // Événements
        return alerts.where((a) => a.isEventAlert).toList();
      case 2: // Maintenance
        return alerts.where((a) => a.isMaintenanceAlert).toList();
      case 3: // Équipement
        return alerts.where((a) => a.isEquipmentAlert).toList();
      default:
        return alerts;
    }
  }

  Widget _buildEmptyState() {
    String message;
    IconData icon;

    switch (_tabController.index) {
      case 1:
        message = 'Aucune alerte d\'événement';
        icon = Icons.event;
        break;
      case 2:
        message = 'Aucune alerte de maintenance';
        icon = Icons.build;
        break;
      case 3:
        message = 'Aucune alerte d\'équipement';
        icon = Icons.inventory_2;
        break;
      default:
        message = 'Aucune notification';
        icon = Icons.notifications_none;
    }

    return Center(
      child: Column(
        mainAxisAlignment: MainAxisAlignment.center,
        children: [
          Icon(icon, size: 64, color: Colors.grey.shade400),
          const SizedBox(height: 16),
          Text(
            message,
            style: TextStyle(
              fontSize: 16,
              color: Colors.grey.shade600,
            ),
          ),
        ],
      ),
    );
  }

  Future<void> _handleAlertTap(AlertModel alert) async {
    // Marquer comme lu si pas déjà lu
    if (!alert.isRead) {
      await _markAsRead(alert.id);
    }

    // Redirection selon actionUrl (pour l'instant, juste rester sur la page)
    // TODO: Implémenter navigation vers événement/équipement si besoin
  }

  Future<void> _markAsRead(String alertId) async {
    try {
      await _alertService.markAsRead(alertId);
    } catch (e) {
      if (mounted) {
        ScaffoldMessenger.of(context).showSnackBar(
          SnackBar(
            content: Text('Erreur : $e'),
            backgroundColor: Colors.red,
          ),
        );
      }
    }
  }

  Future<void> _deleteAlert(String alertId) async {
    try {
      await _alertService.deleteAlert(alertId);
      if (mounted) {
        ScaffoldMessenger.of(context).showSnackBar(
          const SnackBar(
            content: Text('Alerte supprimée'),
            backgroundColor: Colors.green,
          ),
        );
      }
    } catch (e) {
      if (mounted) {
        ScaffoldMessenger.of(context).showSnackBar(
          SnackBar(
            content: Text('Erreur : $e'),
            backgroundColor: Colors.red,
          ),
        );
      }
    }
  }

  Future<void> _markAllAsRead(String userId) async {
    try {
      final alerts = await _alertService.getAlertsForUser(userId);
      for (final alert in alerts.where((a) => !a.isRead)) {
        await _alertService.markAsRead(alert.id);
      }
      if (mounted) {
        ScaffoldMessenger.of(context).showSnackBar(
          const SnackBar(
            content: Text('Toutes les alertes ont été marquées comme lues'),
            backgroundColor: Colors.green,
          ),
        );
      }
    } catch (e) {
      if (mounted) {
        ScaffoldMessenger.of(context).showSnackBar(
          SnackBar(
            content: Text('Erreur : $e'),
            backgroundColor: Colors.red,
          ),
        );
      }
    }
  }
}

