import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import 'package:em2rp/models/event_model.dart';
import 'package:em2rp/providers/event_provider.dart';
import 'package:em2rp/providers/local_user_provider.dart';
import 'package:em2rp/views/event_preparation_page.dart';
import 'package:em2rp/services/api_service.dart';
import 'package:em2rp/utils/colors.dart';

/// Boutons de préparation et retour d'événement
class EventPreparationButtons extends StatefulWidget {
  final EventModel event;

  const EventPreparationButtons({
    super.key,
    required this.event,
  });

  @override
  State<EventPreparationButtons> createState() => _EventPreparationButtonsState();
}

class _EventPreparationButtonsState extends State<EventPreparationButtons> {
  @override
  Widget build(BuildContext context) {
    // Utiliser le provider pour récupérer l'événement à jour
    final eventProvider = context.watch<EventProvider>();

    // Chercher l'événement mis à jour dans le provider
    final EventModel currentEvent = eventProvider.events.firstWhere(
      (e) => e.id == widget.event.id,
      orElse: () => widget.event,
    );

    return _buildButtons(context, currentEvent);
  }


  Widget _buildButtons(BuildContext context, EventModel event) {
    // Vérifier s'il y a du matériel assigné
    final hasMaterial = event.assignedEquipment.isNotEmpty || event.assignedContainers.isNotEmpty;

    if (!hasMaterial) {
      return const SizedBox.shrink();
    }

    // Déterminer l'étape actuelle
    final prep = event.preparationStatus ?? PreparationStatus.notStarted;
    final loading = event.loadingStatus ?? LoadingStatus.notStarted;
    final unloading = event.unloadingStatus ?? UnloadingStatus.notStarted;
    final returnStatus = event.returnStatus ?? ReturnStatus.notStarted;

    String buttonText;
    IconData buttonIcon;
    bool isCompleted = false;

    if (prep != PreparationStatus.completed && prep != PreparationStatus.completedWithMissing) {
      buttonText = 'Préparation dépôt';
      buttonIcon = Icons.inventory_2;
    } else if (loading != LoadingStatus.completed && loading != LoadingStatus.completedWithMissing) {
      buttonText = 'Chargement aller';
      buttonIcon = Icons.local_shipping;
    } else if (unloading != UnloadingStatus.completed && unloading != UnloadingStatus.completedWithMissing) {
      buttonText = 'Chargement retour';
      buttonIcon = Icons.unarchive;
    } else if (returnStatus != ReturnStatus.completed && returnStatus != ReturnStatus.completedWithMissing) {
      buttonText = 'Retour dépôt';
      buttonIcon = Icons.assignment_return;
    } else {
      buttonText = 'Terminé';
      buttonIcon = Icons.check_circle;
      isCompleted = true;
    }

    return Padding(
      padding: const EdgeInsets.symmetric(vertical: 8.0),
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.stretch,
        children: [
          // Bouton de l'étape actuelle
          if (!isCompleted)
            Center(
              child: ConstrainedBox(
                constraints: const BoxConstraints(maxWidth: 400),
                child: ElevatedButton.icon(
                  onPressed: () async {
                    final result = await Navigator.push(
                      context,
                      MaterialPageRoute(
                        builder: (context) => EventPreparationPage(
                          initialEvent: event,
                        ),
                      ),
                    );

                    // Si la validation a réussi, le StreamBuilder se rechargera automatiquement
                    if (result == true && context.mounted) {
                      ScaffoldMessenger.of(context).showSnackBar(
                        const SnackBar(
                          content: Text('Étape validée avec succès'),
                          backgroundColor: Colors.green,
                        ),
                      );
                    }
                  },
                  icon: Icon(buttonIcon),
                  label: Text(
                    buttonText,
                    style: const TextStyle(
                      fontSize: 16,
                      fontWeight: FontWeight.bold,
                    ),
                  ),
                  style: ElevatedButton.styleFrom(
                    backgroundColor: AppColors.bleuFonce,
                    padding: const EdgeInsets.symmetric(vertical: 12, horizontal: 24),
                    shape: RoundedRectangleBorder(
                      borderRadius: BorderRadius.circular(8),
                    ),
                  ),
                ),
              ),
            ),

          // Indicateur de completion
          if (isCompleted)
            Padding(
              padding: const EdgeInsets.only(top: 8.0),
              child: Container(
                padding: const EdgeInsets.all(8),
                decoration: BoxDecoration(
                  color: Colors.green.shade100,
                  borderRadius: BorderRadius.circular(8),
                  border: Border.all(color: Colors.green, width: 1),
                ),
                child: const Row(
                  mainAxisAlignment: MainAxisAlignment.center,
                  children: [
                    Icon(Icons.check_circle, color: Colors.green, size: 20),
                    SizedBox(width: 8),
                    Text(
                      'Toutes les étapes sont terminées',
                      style: TextStyle(
                        color: Colors.green,
                        fontWeight: FontWeight.bold,
                      ),
                    ),
                  ],
                ),
              ),
            ),

          // Bouton de retour en arrière si au moins une étape est commencée/validée
          if (prep != PreparationStatus.notStarted) ...[
            const SizedBox(height: 8),
            Center(
              child: ConstrainedBox(
                constraints: const BoxConstraints(maxWidth: 400),
                child: OutlinedButton.icon(
                  onPressed: () => _showRollbackDialog(context, event),
                  icon: const Icon(Icons.undo, size: 18),
                  label: const Text('Revenir à une étape précédente'),
                  style: OutlinedButton.styleFrom(
                    foregroundColor: Colors.orange[800],
                    side: BorderSide(color: Colors.orange[300]!),
                    padding: const EdgeInsets.symmetric(vertical: 8, horizontal: 16),
                  ),
                ),
              ),
            ),
          ],
        ],
      ),
    );
  }

  Future<void> _showRollbackDialog(BuildContext context, EventModel event) async {
    final prep = event.preparationStatus ?? PreparationStatus.notStarted;
    final loading = event.loadingStatus ?? LoadingStatus.notStarted;
    final unloading = event.unloadingStatus ?? UnloadingStatus.notStarted;
    final returnStatus = event.returnStatus ?? ReturnStatus.notStarted;

    final List<Map<String, String>> steps = [];

    if (prep == PreparationStatus.completed || prep == PreparationStatus.completedWithMissing) {
      steps.add({'key': 'PREPARATION', 'label': 'Préparation dépôt'});
    }
    if (loading == LoadingStatus.completed || loading == LoadingStatus.completedWithMissing) {
      steps.add({'key': 'LOADING', 'label': 'Chargement aller'});
    }
    if (unloading == UnloadingStatus.completed || unloading == UnloadingStatus.completedWithMissing) {
      steps.add({'key': 'UNLOADING', 'label': 'Chargement retour'});
    }
    if (returnStatus == ReturnStatus.completed || returnStatus == ReturnStatus.completedWithMissing) {
      steps.add({'key': 'RETURN', 'label': 'Retour dépôt'});
    }

    if (steps.isEmpty) return;

    final String? selectedStep = await showDialog<String>(
      context: context,
      builder: (BuildContext context) {
        return AlertDialog(
          title: const Row(
            children: [
              Icon(Icons.undo, color: Colors.orange),
              SizedBox(width: 8),
              Text('Revenir en arrière'),
            ],
          ),
          content: Column(
            mainAxisSize: MainAxisSize.min,
            crossAxisAlignment: CrossAxisAlignment.start,
            children: [
              const Text('Sélectionnez l\'étape à laquelle vous souhaitez revenir :'),
              const SizedBox(height: 12),
              ...steps.map((step) {
                return ListTile(
                  leading: const Icon(Icons.arrow_back, color: AppColors.rouge),
                  title: Text(step['label']!),
                  onTap: () => Navigator.of(context).pop(step['key']),
                );
              }),
            ],
          ),
          actions: [
            TextButton(
              onPressed: () => Navigator.of(context).pop(),
              child: const Text('Annuler'),
            ),
          ],
        );
      },
    );

    if (selectedStep != null && context.mounted) {
      final confirm = await showDialog<bool>(
        context: context,
        builder: (BuildContext context) {
          return AlertDialog(
            title: const Text('Confirmer le retour en arrière'),
            content: const Text(
              'Êtes-vous sûr de vouloir revenir à cette étape ?\n\n'
              'Toutes les validations des étapes ultérieures seront effacées, '
              'et si le retour était terminé, les stocks restaurés seront annulés.'
            ),
            actions: [
              TextButton(
                onPressed: () => Navigator.of(context).pop(false),
                child: const Text('Annuler'),
              ),
              ElevatedButton(
                onPressed: () => Navigator.of(context).pop(true),
                style: ElevatedButton.styleFrom(
                  backgroundColor: AppColors.rouge,
                ),
                child: const Text('Confirmer'),
              ),
            ],
          );
        },
      );

      if (confirm == true && context.mounted) {
        // Utiliser le rootNavigator pour s'assurer qu'on gère la bonne pile de navigation
        final navigator = Navigator.of(context, rootNavigator: true);
        
        showDialog(
          context: context,
          barrierDismissible: false,
          useRootNavigator: true,
          builder: (BuildContext dialogContext) {
            return const PopScope(
              canPop: false,
              child: Center(child: CircularProgressIndicator()),
            );
          },
        );

        try {
          final apiService = FirebaseFunctionsApiService();
          await apiService.call('rollbackEventStep', {
            'eventId': event.id,
            'targetStep': selectedStep,
          });

          // Attendre un tout petit peu pour s'assurer que le showDialog a eu le temps
          // de faire son animation d'entrée si l'API a répondu trop vite.
          await Future.delayed(const Duration(milliseconds: 200));

          if (context.mounted) {
            final eventProvider = Provider.of<EventProvider>(context, listen: false);
            final userProvider = Provider.of<LocalUserProvider>(context, listen: false);
            if (userProvider.currentUser != null) {
              await eventProvider.refreshEvents(userProvider.currentUser!.uid);
            }
          }

          navigator.pop(); // Fermer le loader

          if (context.mounted) {
            ScaffoldMessenger.of(context).showSnackBar(
              const SnackBar(
                content: Text('Retour en arrière effectué avec succès'),
                backgroundColor: Colors.green,
              ),
            );
          }
        } catch (e) {
          navigator.pop(); // Fermer le loader
          
          if (context.mounted) {
            ScaffoldMessenger.of(context).showSnackBar(
              SnackBar(
                content: Text('Erreur : $e'),
                backgroundColor: Colors.red,
              ),
            );
          }
        }
      }
    }
  }
}

