import 'package:flutter/material.dart';
import 'package:em2rp/models/container_model.dart';
import 'package:em2rp/models/equipment_model.dart';
import 'package:em2rp/models/event_model.dart';
import 'package:em2rp/views/widgets/equipment/equipment_checklist_item.dart';
import 'package:em2rp/utils/colors.dart';

/// Widget pour afficher un container avec ses équipements enfants dans une checklist
class ContainerChecklistItem extends StatelessWidget {
  final ContainerModel container;
  final List<EquipmentModel> childEquipments;
  final Map<String, EventEquipment> eventEquipmentsMap; // Map equipmentId -> EventEquipment
  final ChecklistStep step;
  final bool isValidated; // Tous les enfants validés
  final Map<String, bool> childValidationStates;
  final VoidCallback onToggleContainer; // Callback pour clic sur le container (valide tout)
  final Function(String equipmentId) onToggleChild;
  final Function(String equipmentId, int quantity)? onQuantityChanged;
  final Map<String, bool> wasMissingBeforeMap; // Map des enfants manquants à l'étape précédente

  const ContainerChecklistItem({
    super.key,
    required this.container,
    required this.childEquipments,
    required this.eventEquipmentsMap,
    required this.step,
    required this.isValidated,
    required this.childValidationStates,
    required this.onToggleContainer,
    required this.onToggleChild,
    this.onQuantityChanged,
    required this.wasMissingBeforeMap,
  });

  @override
  Widget build(BuildContext context) {
    final validatedCount = childValidationStates.values.where((v) => v).length;
    final totalCount = childEquipments.length;

    return Column(
      crossAxisAlignment: CrossAxisAlignment.stretch,
      children: [
        // Header du container (cliquable pour tout valider)
        Card(
          margin: const EdgeInsets.symmetric(vertical: 4, horizontal: 0),
          elevation: 2,
          color: isValidated ? Colors.green.shade50 : Colors.white,
          shape: RoundedRectangleBorder(
            borderRadius: BorderRadius.circular(8),
            side: BorderSide(
              color: isValidated ? Colors.green : AppColors.rouge.withValues(alpha: 0.3),
              width: 2,
            ),
          ),
          child: ListTile(
            onTap: onToggleContainer, // Clic sur le container = valider tout son contenu
            leading: Container(
              width: 40,
              height: 40,
              decoration: BoxDecoration(
                color: AppColors.rouge.withValues(alpha: 0.1),
                borderRadius: BorderRadius.circular(8),
              ),
              child: Icon(
                container.type.iconData,
                color: AppColors.rouge,
                size: 24,
              ),
            ),
            title: Text(
              container.name,
              style: const TextStyle(
                fontWeight: FontWeight.bold,
                fontSize: 16,
              ),
            ),
            subtitle: Text(
              '$validatedCount / $totalCount équipement(s) validé(s)\nCliquer pour tout valider',
              style: TextStyle(
                fontSize: 13,
                color: Colors.grey.shade700,
              ),
            ),
            trailing: Icon(
              isValidated ? Icons.check_circle : Icons.inventory,
              color: isValidated ? Colors.green : AppColors.rouge,
              size: 28,
            ),
          ),
        ),

        // Enfants (équipements) indentés
        ...childEquipments.map((equipment) {
          final eventEquipment = eventEquipmentsMap[equipment.id];
          if (eventEquipment == null) return const SizedBox.shrink();

          return EquipmentChecklistItem(
            equipment: equipment,
            eventEquipment: eventEquipment,
            step: step,
            isValidated: childValidationStates[equipment.id] ?? false,
            onToggle: () => onToggleChild(equipment.id),
            onQuantityChanged: onQuantityChanged != null
                ? (qty) => onQuantityChanged!(equipment.id, qty)
                : null,
            isChild: true, // Affichage indenté et plus petit
            wasMissingBefore: wasMissingBeforeMap[equipment.id] ?? false,
          );
        }),
      ],
    );
  }
}

