import 'package:flutter/material.dart';
import 'package:em2rp/models/equipment_model.dart';
import 'package:em2rp/models/event_model.dart';
import 'package:em2rp/utils/colors.dart';

/// Dialog affichant les équipements manquants lors de la préparation/retour
class MissingEquipmentDialog extends StatelessWidget {
  final List<EquipmentModel> missingEquipment;
  final List<EventEquipment> eventEquipment;
  final bool isReturnMode;

  const MissingEquipmentDialog({
    super.key,
    required this.missingEquipment,
    required this.eventEquipment,
    this.isReturnMode = false,
  });

  @override
  Widget build(BuildContext context) {
    return Dialog(
      shape: RoundedRectangleBorder(
        borderRadius: BorderRadius.circular(16),
      ),
      child: Container(
        constraints: const BoxConstraints(maxWidth: 600, maxHeight: 600),
        padding: const EdgeInsets.all(24),
        child: Column(
          mainAxisSize: MainAxisSize.min,
          children: [
            // En-tête avec icône warning
            Row(
              children: [
                Container(
                  padding: const EdgeInsets.all(12),
                  decoration: BoxDecoration(
                    color: Colors.orange.shade100,
                    shape: BoxShape.circle,
                  ),
                  child: Icon(
                    Icons.warning_amber_rounded,
                    size: 32,
                    color: Colors.orange.shade700,
                  ),
                ),
                const SizedBox(width: 16),
                Expanded(
                  child: Column(
                    crossAxisAlignment: CrossAxisAlignment.start,
                    children: [
                      Text(
                        isReturnMode
                            ? 'Équipements manquants au retour'
                            : 'Équipements manquants',
                        style: const TextStyle(
                          fontSize: 20,
                          fontWeight: FontWeight.bold,
                        ),
                      ),
                      const SizedBox(height: 4),
                      Text(
                        '${missingEquipment.length} équipement(s) non validé(s)',
                        style: TextStyle(
                          fontSize: 14,
                          color: Colors.grey.shade700,
                        ),
                      ),
                    ],
                  ),
                ),
              ],
            ),

            const SizedBox(height: 16),
            const Divider(),
            const SizedBox(height: 8),

            // Liste des équipements manquants
            Flexible(
              child: ListView.builder(
                shrinkWrap: true,
                itemCount: missingEquipment.length,
                itemBuilder: (context, index) {
                  final equipment = missingEquipment[index];
                  final eventEq = eventEquipment.firstWhere(
                    (eq) => eq.equipmentId == equipment.id,
                    orElse: () => EventEquipment(equipmentId: equipment.id),
                  );

                  return Card(
                    margin: const EdgeInsets.only(bottom: 8),
                    child: ListTile(
                      leading: CircleAvatar(
                        backgroundColor: Colors.orange.shade100,
                        child: equipment.category.getIcon(
                          size: 20,
                          color: Colors.orange.shade700,
                        ),
                      ),
                      title: Text(
                        equipment.name,
                        style: const TextStyle(fontWeight: FontWeight.w600),
                      ),
                      subtitle: equipment.hasQuantity
                          ? Text('Quantité : ${eventEq.quantity}')
                          : Text(equipment.model ?? equipment.category.label),
                      trailing: Icon(
                        Icons.error_outline,
                        color: Colors.orange.shade700,
                      ),
                    ),
                  );
                },
              ),
            ),

            const SizedBox(height: 16),
            const Divider(),
            const SizedBox(height: 8),

            // Actions
            Column(
              crossAxisAlignment: CrossAxisAlignment.stretch,
              children: [
                // Bouton principal : Confirmer malgré les manquants
                ElevatedButton(
                  onPressed: () => Navigator.of(context).pop('confirm_anyway'),
                  style: ElevatedButton.styleFrom(
                    backgroundColor: AppColors.rouge,
                    padding: const EdgeInsets.symmetric(vertical: 16),
                    shape: RoundedRectangleBorder(
                      borderRadius: BorderRadius.circular(8),
                    ),
                  ),
                  child: Text(
                    isReturnMode
                        ? 'Confirmer le retour malgré les manquants'
                        : 'Confirmer malgré les manquants',
                    style: const TextStyle(
                      fontSize: 16,
                      fontWeight: FontWeight.bold,
                    ),
                  ),
                ),

                const SizedBox(height: 12),

                // Bouton secondaire : Marquer comme validés
                OutlinedButton(
                  onPressed: () => Navigator.of(context).pop('mark_as_validated'),
                  style: OutlinedButton.styleFrom(
                    padding: const EdgeInsets.symmetric(vertical: 16),
                    side: BorderSide(color: AppColors.bleuFonce, width: 2),
                    shape: RoundedRectangleBorder(
                      borderRadius: BorderRadius.circular(8),
                    ),
                  ),
                  child: Text(
                    'Indiquer les manquants comme validés',
                    style: TextStyle(
                      fontSize: 16,
                      fontWeight: FontWeight.bold,
                      color: AppColors.bleuFonce,
                    ),
                  ),
                ),

                const SizedBox(height: 12),

                // Bouton tertiaire : Retourner à la liste
                TextButton(
                  onPressed: () => Navigator.of(context).pop('return_to_list'),
                  style: TextButton.styleFrom(
                    padding: const EdgeInsets.symmetric(vertical: 16),
                  ),
                  child: const Text(
                    'Retourner à la liste',
                    style: TextStyle(
                      fontSize: 16,
                      color: Colors.grey,
                    ),
                  ),
                ),
              ],
            ),
          ],
        ),
      ),
    );
  }
}

