import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import 'package:em2rp/utils/colors.dart';
import 'package:em2rp/models/container_model.dart';
import 'package:em2rp/models/equipment_model.dart';
import 'package:em2rp/providers/container_provider.dart';
import 'package:em2rp/providers/equipment_provider.dart';
import 'package:em2rp/utils/debug_log.dart';
import 'package:em2rp/utils/id_generator.dart';
import 'package:em2rp/services/data_service.dart';
import 'package:em2rp/services/api_service.dart';
import 'package:em2rp/utils/debouncer.dart';
import 'package:em2rp/views/widgets/nav/custom_app_bar.dart';

class ContainerFormPage extends StatefulWidget {
  final ContainerModel? container;

  const ContainerFormPage({super.key, this.container});

  @override
  State<ContainerFormPage> createState() => _ContainerFormPageState();
}

class _ContainerFormPageState extends State<ContainerFormPage> {
  final _formKey = GlobalKey<FormState>();

  // Controllers
  final _nameController = TextEditingController();
  final _idController = TextEditingController();
  final _weightController = TextEditingController();
  final _lengthController = TextEditingController();
  final _widthController = TextEditingController();
  final _heightController = TextEditingController();
  final _notesController = TextEditingController();

  // Form fields
  ContainerType _selectedType = ContainerType.flightCase;
  EquipmentStatus _selectedStatus = EquipmentStatus.available;
  final ValueNotifier<bool> _autoGenerateIdNotifier = ValueNotifier<bool>(true);
  final Set<String> _selectedEquipmentIds = {};

  bool _isEditing = false;

  @override
  void initState() {
    super.initState();
    if (widget.container != null) {
      _isEditing = true;
      _loadContainerData();
    }
  }

  void _loadContainerData() {
    final container = widget.container!;
    _nameController.text = container.name;
    _idController.text = container.id;
    _selectedType = container.type;
    _selectedStatus = container.status;
    _weightController.text = container.weight?.toString() ?? '';
    _lengthController.text = container.length?.toString() ?? '';
    _widthController.text = container.width?.toString() ?? '';
    _heightController.text = container.height?.toString() ?? '';
    _notesController.text = container.notes ?? '';
    _selectedEquipmentIds.addAll(container.equipmentIds);
    _autoGenerateIdNotifier.value = false;
  }

  void _updateIdFromName() {
    if (_autoGenerateIdNotifier.value && !_isEditing) {
      final name = _nameController.text;
      if (name.isNotEmpty) {
        final baseId = IdGenerator.generateContainerId(
          type: _selectedType,
          name: name,
        );
        _idController.text = baseId;
      }
    }
  }

  void _updateIdFromType() {
    if (_autoGenerateIdNotifier.value && !_isEditing) {
      final name = _nameController.text;
      if (name.isNotEmpty) {
        final baseId = IdGenerator.generateContainerId(
          type: _selectedType,
          name: name,
        );
        _idController.text = baseId;
      }
    }
  }

  Widget _buildCard({
    required String title,
    required IconData icon,
    required List<Widget> children,
  }) {
    return Card(
      elevation: 2,
      shadowColor: Colors.black12,
      shape: RoundedRectangleBorder(
        borderRadius: BorderRadius.circular(12),
        side: BorderSide(color: Colors.grey.shade200, width: 1),
      ),
      child: Padding(
        padding: const EdgeInsets.all(20.0),
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.stretch,
          children: [
            Row(
              children: [
                Icon(icon, color: AppColors.rouge, size: 20),
                const SizedBox(width: 8),
                Text(
                  title,
                  style: const TextStyle(
                    fontSize: 16,
                    fontWeight: FontWeight.bold,
                    color: AppColors.noir,
                  ),
                ),
              ],
            ),
            const Divider(height: 24, thickness: 1),
            ...children,
          ],
        ),
      ),
    );
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: CustomAppBar(
        title: _isEditing ? 'Modifier boîte' : 'Nouvelle boîte',
      ),
      body: Center(
        child: ConstrainedBox(
          constraints: const BoxConstraints(maxWidth: 800),
          child: SingleChildScrollView(
            padding: const EdgeInsets.all(24),
            child: Form(
              key: _formKey,
              child: Column(
                crossAxisAlignment: CrossAxisAlignment.stretch,
                children: [
                  // Card 1: Informations Générales
                  _buildCard(
                    title: 'Informations générales',
                    icon: Icons.info_outline,
                    children: [
                      // Nom
                      TextFormField(
                        controller: _nameController,
                        decoration: const InputDecoration(
                          labelText: 'Nom du container *',
                          hintText: 'ex: Flight Case Beam 7R',
                          border: OutlineInputBorder(),
                          prefixIcon: Icon(Icons.label),
                        ),
                        onChanged: (_) => _updateIdFromName(),
                        validator: (value) {
                          if (value == null || value.isEmpty) {
                            return 'Veuillez entrer un nom';
                          }
                          return null;
                        },
                      ),
                      const SizedBox(height: 16),

                      // ID
                      ValueListenableBuilder<bool>(
                        valueListenable: _autoGenerateIdNotifier,
                        builder: (context, autoGenerateId, child) {
                          return Row(
                            crossAxisAlignment: CrossAxisAlignment.start,
                            children: [
                              Expanded(
                                child: TextFormField(
                                  controller: _idController,
                                  decoration: const InputDecoration(
                                    labelText: 'Identifiant *',
                                    hintText: 'ex: FLIGHTCASE_BEAM',
                                    border: OutlineInputBorder(),
                                    prefixIcon: Icon(Icons.qr_code),
                                  ),
                                  enabled: !autoGenerateId || _isEditing,
                                  validator: (value) {
                                    if (value == null || value.isEmpty) {
                                      return 'Veuillez entrer un identifiant';
                                    }
                                    final validation = IdGenerator.validateContainerId(value);
                                    return validation;
                                  },
                                ),
                              ),
                              if (!_isEditing) ...[
                                const SizedBox(width: 8),
                                IconButton(
                                  icon: Icon(
                                    autoGenerateId ? Icons.lock : Icons.lock_open,
                                    color: autoGenerateId ? AppColors.rouge : Colors.grey,
                                  ),
                                  tooltip: autoGenerateId
                                      ? 'Génération automatique'
                                      : 'Saisie manuelle',
                                  onPressed: () {
                                    _autoGenerateIdNotifier.value = !autoGenerateId;
                                    if (_autoGenerateIdNotifier.value) {
                                      _updateIdFromName();
                                    }
                                  },
                                ),
                              ],
                            ],
                          );
                        },
                      ),
                      const SizedBox(height: 16),

                      // Type
                      DropdownButtonFormField<ContainerType>(
                        initialValue: _selectedType,
                        decoration: const InputDecoration(
                          labelText: 'Type de container *',
                          border: OutlineInputBorder(),
                          prefixIcon: Icon(Icons.category),
                        ),
                        items: ContainerType.values.map((type) {
                          return DropdownMenuItem(
                            value: type,
                            child: Row(
                              children: [
                                type.getIcon(size: 20, color: AppColors.rouge),
                                const SizedBox(width: 8),
                                Text(type.label),
                              ],
                            ),
                          );
                        }).toList(),
                        onChanged: (value) {
                          if (value != null) {
                            setState(() {
                              _selectedType = value;
                              _updateIdFromType();
                            });
                          }
                        },
                      ),
                      const SizedBox(height: 16),

                      // Statut
                      DropdownButtonFormField<EquipmentStatus>(
                        initialValue: _selectedStatus,
                        decoration: const InputDecoration(
                          labelText: 'Statut *',
                          border: OutlineInputBorder(),
                          prefixIcon: Icon(Icons.info_outline),
                        ),
                        items: [
                          EquipmentStatus.available,
                          EquipmentStatus.inUse,
                          EquipmentStatus.maintenance,
                          EquipmentStatus.outOfService,
                        ].map((status) {
                          String label;
                          switch (status) {
                            case EquipmentStatus.available:
                              label = 'Disponible';
                              break;
                            case EquipmentStatus.inUse:
                              label = 'En prestation';
                              break;
                            case EquipmentStatus.maintenance:
                              label = 'En maintenance';
                              break;
                            case EquipmentStatus.outOfService:
                              label = 'Hors service';
                              break;
                            default:
                              label = 'Autre';
                          }
                          return DropdownMenuItem(
                            value: status,
                            child: Text(label),
                          );
                        }).toList(),
                        onChanged: (value) {
                          if (value != null) {
                            setState(() {
                              _selectedStatus = value;
                            });
                          }
                        },
                      ),
                    ],
                  ),
                  const SizedBox(height: 20),

                  // Card 2: Caractéristiques Physiques
                  _buildCard(
                    title: 'Caractéristiques physiques',
                    icon: Icons.scale_outlined,
                    children: [
                      // Poids
                      TextFormField(
                        controller: _weightController,
                        decoration: const InputDecoration(
                          labelText: 'Poids à vide (kg)',
                          hintText: 'ex: 15.5',
                          border: OutlineInputBorder(),
                          prefixIcon: Icon(Icons.scale),
                        ),
                        keyboardType: const TextInputType.numberWithOptions(decimal: true),
                        validator: (value) {
                          if (value != null && value.isNotEmpty) {
                            if (double.tryParse(value) == null) {
                              return 'Veuillez entrer un nombre valide';
                            }
                          }
                          return null;
                        },
                      ),
                      const SizedBox(height: 16),

                      // Dimensions
                      Row(
                        children: [
                          Expanded(
                            child: TextFormField(
                              controller: _lengthController,
                              decoration: const InputDecoration(
                                labelText: 'Longueur (cm)',
                                border: OutlineInputBorder(),
                              ),
                              keyboardType: const TextInputType.numberWithOptions(decimal: true),
                              validator: (value) {
                                if (value != null && value.isNotEmpty) {
                                  if (double.tryParse(value) == null) {
                                    return 'Nombre invalide';
                                  }
                                }
                                return null;
                              },
                            ),
                          ),
                          const SizedBox(width: 8),
                          Expanded(
                            child: TextFormField(
                              controller: _widthController,
                              decoration: const InputDecoration(
                                labelText: 'Largeur (cm)',
                                border: OutlineInputBorder(),
                              ),
                              keyboardType: const TextInputType.numberWithOptions(decimal: true),
                              validator: (value) {
                                if (value != null && value.isNotEmpty) {
                                  if (double.tryParse(value) == null) {
                                    return 'Nombre invalide';
                                  }
                                }
                                return null;
                              },
                            ),
                          ),
                          const SizedBox(width: 8),
                          Expanded(
                            child: TextFormField(
                              controller: _heightController,
                              decoration: const InputDecoration(
                                labelText: 'Hauteur (cm)',
                                border: OutlineInputBorder(),
                              ),
                              keyboardType: const TextInputType.numberWithOptions(decimal: true),
                              validator: (value) {
                                if (value != null && value.isNotEmpty) {
                                  if (double.tryParse(value) == null) {
                                    return 'Nombre invalide';
                                  }
                                }
                                return null;
                              },
                            ),
                          ),
                        ],
                      ),
                    ],
                  ),
                  const SizedBox(height: 20),

                  // Card 3: Équipements dans ce container
                  _buildCard(
                    title: 'Équipements dans ce container',
                    icon: Icons.inventory_2_outlined,
                    children: [
                      if (_selectedEquipmentIds.isNotEmpty)
                        Container(
                          padding: const EdgeInsets.all(12),
                          decoration: BoxDecoration(
                            border: Border.all(color: Colors.grey.shade200),
                            borderRadius: BorderRadius.circular(8),
                            color: Colors.grey.shade50,
                          ),
                          child: Column(
                            crossAxisAlignment: CrossAxisAlignment.start,
                            children: [
                              Text(
                                '${_selectedEquipmentIds.length} équipement(s) sélectionné(s) :',
                                style: TextStyle(
                                  fontSize: 13,
                                  fontWeight: FontWeight.bold,
                                  color: Colors.grey.shade700,
                                ),
                              ),
                              const SizedBox(height: 8),
                              Wrap(
                                spacing: 8,
                                runSpacing: 8,
                                children: _selectedEquipmentIds.map((id) {
                                  return Chip(
                                    label: Text(
                                      id,
                                      style: const TextStyle(fontSize: 12, fontWeight: FontWeight.w500),
                                    ),
                                    deleteIcon: const Icon(Icons.close, size: 16),
                                    onDeleted: () {
                                      setState(() {
                                        _selectedEquipmentIds.remove(id);
                                      });
                                    },
                                    backgroundColor: AppColors.rouge.withValues(alpha: 0.08),
                                    side: BorderSide(color: AppColors.rouge.withValues(alpha: 0.2)),
                                    materialTapTargetSize: MaterialTapTargetSize.shrinkWrap,
                                  );
                                }).toList(),
                              ),
                            ],
                          ),
                        )
                      else
                        Container(
                          padding: const EdgeInsets.all(20),
                          decoration: BoxDecoration(
                            border: Border.all(color: Colors.grey.shade200),
                            borderRadius: BorderRadius.circular(8),
                            color: Colors.grey.shade50,
                          ),
                          child: const Center(
                            child: Text(
                              'Aucun équipement sélectionné',
                              style: TextStyle(color: Colors.grey),
                            ),
                          ),
                        ),
                      const SizedBox(height: 16),

                      // Bouton pour ajouter des équipements
                      OutlinedButton.icon(
                        onPressed: _selectEquipment,
                        icon: const Icon(Icons.add),
                        label: const Text('Ajouter des équipements'),
                        style: OutlinedButton.styleFrom(
                          minimumSize: const Size(double.infinity, 48),
                          side: BorderSide(color: AppColors.rouge),
                          foregroundColor: AppColors.rouge,
                        ),
                      ),
                    ],
                  ),
                  const SizedBox(height: 20),

                  // Card 4: Notes
                  _buildCard(
                    title: 'Notes & Remarques',
                    icon: Icons.notes_outlined,
                    children: [
                      TextFormField(
                        controller: _notesController,
                        decoration: const InputDecoration(
                          labelText: 'Notes complémentaires',
                          hintText: 'Informations additionnelles...',
                          border: OutlineInputBorder(),
                          prefixIcon: Icon(Icons.edit_note),
                        ),
                        maxLines: 3,
                      ),
                    ],
                  ),
                  const SizedBox(height: 32),

                  // Actions
                  Row(
                    mainAxisAlignment: MainAxisAlignment.end,
                    children: [
                      TextButton(
                        onPressed: () => Navigator.pop(context),
                        child: const Text('Annuler', style: TextStyle(fontSize: 16)),
                      ),
                      const SizedBox(width: 16),
                      ElevatedButton.icon(
                        onPressed: _saveContainer,
                        icon: const Icon(Icons.save, color: Colors.white),
                        label: Text(
                          _isEditing ? 'Enregistrer' : 'Créer',
                          style: const TextStyle(color: Colors.white, fontSize: 16, fontWeight: FontWeight.bold),
                        ),
                        style: ElevatedButton.styleFrom(
                          backgroundColor: AppColors.rouge,
                          padding: const EdgeInsets.symmetric(horizontal: 32, vertical: 16),
                          shape: RoundedRectangleBorder(
                            borderRadius: BorderRadius.circular(8),
                          ),
                        ),
                      ),
                    ],
                  ),
                  const SizedBox(height: 24),
                ],
              ),
            ),
          ),
        ),
      ),
    );
  }

  Future<void> _selectEquipment() async {
    final equipmentProvider = context.read<EquipmentProvider>();

    // Toujours charger la liste complète pour éviter d'afficher uniquement
    // la page paginée active d'un autre écran.
    await equipmentProvider.loadEquipments();
    if (!mounted) return;

    await showDialog(
      context: context,
      builder: (context) => _EquipmentSelectorDialog(
        selectedIds: _selectedEquipmentIds,
        equipmentProvider: equipmentProvider,
      ),
    );

    if (!mounted) return;
    setState(() {});
  }

  Future<bool> _isIdUnique(String id) async {
    final provider = context.read<ContainerProvider>();
    final container = await provider.getContainerById(id);
    return container == null;
  }

  Future<void> _saveContainer() async {
    if (!_formKey.currentState!.validate()) {
      return;
    }

    try {
      final containerProvider = context.read<ContainerProvider>();

      if (_isEditing) {
        await _updateContainer(containerProvider);
      } else {
        await _createSingleContainer(containerProvider);
      }
    } catch (e) {
      if (mounted) {
        ScaffoldMessenger.of(context).showSnackBar(
          SnackBar(content: Text('Erreur: $e')),
        );
      }
    }
  }

  Future<void> _createSingleContainer(ContainerProvider provider) async {
    final baseId = _idController.text.trim();

    // Vérifier l'unicité de l'ID directement
    String uniqueId = baseId;
    if (!await _isIdUnique(baseId)) {
      uniqueId = '${baseId}_${DateTime.now().millisecondsSinceEpoch}';
    }

    final container = ContainerModel(
      id: uniqueId,
      name: _nameController.text.trim(),
      type: _selectedType,
      status: _selectedStatus,
      equipmentIds: _selectedEquipmentIds.toList(),
      weight: _weightController.text.isNotEmpty
          ? double.tryParse(_weightController.text)
          : null,
      length: _lengthController.text.isNotEmpty
          ? double.tryParse(_lengthController.text)
          : null,
      width: _widthController.text.isNotEmpty
          ? double.tryParse(_widthController.text)
          : null,
      height: _heightController.text.isNotEmpty
          ? double.tryParse(_heightController.text)
          : null,
      notes: _notesController.text.trim().isNotEmpty
          ? _notesController.text.trim()
          : null,
      createdAt: DateTime.now(),
      updatedAt: DateTime.now(),
    );

    await provider.createContainer(container);

    // Mettre à jour les parentBoxIds des équipements
    for (final equipmentId in _selectedEquipmentIds) {
      try {
        await provider.addEquipmentToContainer(
          containerId: uniqueId,
          equipmentId: equipmentId,
        );
      } catch (e) {
        DebugLog.error(
            'Erreur lors de l\'ajout de l\'équipement $equipmentId', e);
      }
    }

    if (mounted) {
      Navigator.pop(context);
      ScaffoldMessenger.of(context).showSnackBar(
        const SnackBar(content: Text('Container créé avec succès')),
      );
    }
  }

  Future<void> _updateContainer(ContainerProvider provider) async {
    final container = widget.container!;

    await provider.updateContainer(container.id, {
      'name': _nameController.text.trim(),
      'type': containerTypeToString(_selectedType),
      'status': equipmentStatusToString(_selectedStatus),
      'equipmentIds': _selectedEquipmentIds.toList(),
      'weight': _weightController.text.isNotEmpty
          ? double.tryParse(_weightController.text)
          : null,
      'length': _lengthController.text.isNotEmpty
          ? double.tryParse(_lengthController.text)
          : null,
      'width': _widthController.text.isNotEmpty
          ? double.tryParse(_widthController.text)
          : null,
      'height': _heightController.text.isNotEmpty
          ? double.tryParse(_heightController.text)
          : null,
      'notes': _notesController.text.trim().isNotEmpty
          ? _notesController.text.trim()
          : null,
    });

    // Gérer les équipements ajoutés
    final addedEquipment =
        _selectedEquipmentIds.difference(container.equipmentIds.toSet());
    for (final equipmentId in addedEquipment) {
      try {
        await provider.addEquipmentToContainer(
          containerId: container.id,
          equipmentId: equipmentId,
        );
      } catch (e) {
        DebugLog.error(
            'Erreur lors de l\'ajout de l\'équipement $equipmentId', e);
      }
    }

    // Gérer les équipements retirés
    final removedEquipment =
        container.equipmentIds.toSet().difference(_selectedEquipmentIds);
    for (final equipmentId in removedEquipment) {
      try {
        await provider.removeEquipmentFromContainer(
          containerId: container.id,
          equipmentId: equipmentId,
        );
      } catch (e) {
        DebugLog.error(
            'Erreur lors du retrait de l\'équipement $equipmentId', e);
      }
    }

    if (mounted) {
      Navigator.pop(context);
      ScaffoldMessenger.of(context).showSnackBar(
        const SnackBar(content: Text('Container mis à jour avec succès')),
      );
    }
  }

  @override
  void dispose() {
    _nameController.dispose();
    _idController.dispose();
    _weightController.dispose();
    _lengthController.dispose();
    _widthController.dispose();
    _heightController.dispose();
    _notesController.dispose();
    _autoGenerateIdNotifier.dispose();
    super.dispose();
  }
}

/// Widget de dialogue pour sélectionner les équipements
class _EquipmentSelectorDialog extends StatefulWidget {
  final Set<String> selectedIds;
  final EquipmentProvider equipmentProvider;

  const _EquipmentSelectorDialog({
    required this.selectedIds,
    required this.equipmentProvider,
  });

  @override
  State<_EquipmentSelectorDialog> createState() =>
      _EquipmentSelectorDialogState();
}

class _EquipmentSelectorDialogState extends State<_EquipmentSelectorDialog> {
  final TextEditingController _searchController = TextEditingController();
  final ScrollController _scrollController = ScrollController();
  final DataService _dataService = DataService(FirebaseFunctionsApiService());

  EquipmentCategory? _filterCategory;
  String _searchQuery = '';
  late Set<String> _tempSelectedIds;
  final _searchDebouncer = Debouncer();

  final List<EquipmentModel> _paginatedEquipments = [];
  bool _isLoadingMore = false;
  bool _hasMoreEquipments = true;
  String? _lastEquipmentId;

  @override
  void initState() {
    super.initState();
    // Créer une copie temporaire des IDs sélectionnés
    _tempSelectedIds = Set<String>.from(widget.selectedIds);
    _scrollController.addListener(_onScroll);
    _loadNextPage();
  }

  @override
  void dispose() {
    _searchController.dispose();
    _scrollController.dispose();
    _searchDebouncer.dispose();
    super.dispose();
  }

  void _onScroll() {
    if (_isLoadingMore) return;
    if (_scrollController.hasClients &&
        _scrollController.position.pixels >= _scrollController.position.maxScrollExtent - 300) {
      if (_hasMoreEquipments) {
        _loadNextPage();
      }
    }
  }

  Future<void> _loadNextPage() async {
    if (_isLoadingMore || !_hasMoreEquipments) return;
    setState(() => _isLoadingMore = true);

    try {
      final result = await _dataService.getEquipmentsPaginated(
        limit: 50,
        startAfter: _lastEquipmentId,
        searchQuery: _searchQuery.isNotEmpty ? _searchQuery : null,
        category: _filterCategory != null ? equipmentCategoryToString(_filterCategory!) : null,
        sortBy: 'id',
        sortOrder: 'asc',
      );

      final newEquipments = (result['equipments'] as List<dynamic>)
          .map((data) => EquipmentModel.fromMap(data as Map<String, dynamic>, data['id'] as String))
          .toList();

      if (mounted) {
        setState(() {
          _paginatedEquipments.addAll(newEquipments);
          _hasMoreEquipments = result['hasMore'] as bool? ?? false;
          _lastEquipmentId = result['lastVisible'] as String?;
          _isLoadingMore = false;
        });
      }
    } catch (e) {
      if (mounted) {
        setState(() => _isLoadingMore = false);
      }
    }
  }

  Future<void> _reloadData() async {
    setState(() {
      _paginatedEquipments.clear();
      _lastEquipmentId = null;
      _hasMoreEquipments = true;
    });
    await _loadNextPage();
  }

  @override
  Widget build(BuildContext context) {
    return Dialog(
      child: Container(
        width: MediaQuery.of(context).size.width * 0.8,
        height: MediaQuery.of(context).size.height * 0.8,
        padding: const EdgeInsets.all(24),
        child: Column(
          children: [
            // En-tête
            Row(
              children: [
                const Icon(Icons.inventory, color: AppColors.rouge),
                const SizedBox(width: 12),
                const Expanded(
                  child: Text(
                    'Sélectionner des équipements',
                    style: TextStyle(
                      fontSize: 20,
                      fontWeight: FontWeight.bold,
                    ),
                  ),
                ),
                IconButton(
                  icon: const Icon(Icons.close),
                  onPressed: () => Navigator.pop(context),
                ),
              ],
            ),
            const Divider(),
            const SizedBox(height: 16),

            // Barre de recherche
            TextField(
              controller: _searchController,
              decoration: InputDecoration(
                hintText: 'Rechercher un équipement...',
                prefixIcon: const Icon(Icons.search),
                border: OutlineInputBorder(
                  borderRadius: BorderRadius.circular(8),
                ),
                suffixIcon: _searchController.text.isNotEmpty
                    ? IconButton(
                        icon: const Icon(Icons.clear),
                        onPressed: () {
                          _searchController.clear();
                          setState(() {
                            _searchQuery = '';
                          });
                          _reloadData();
                        },
                      )
                    : null,
              ),
              onChanged: (value) {
                setState(() {
                  _searchQuery = value;
                });
                _searchDebouncer(_reloadData);
              },
            ),
            const SizedBox(height: 16),

            // Filtres par catégorie
            SizedBox(
              height: 50,
              child: ListView(
                scrollDirection: Axis.horizontal,
                children: [
                  ChoiceChip(
                    label: const Text('Tout'),
                    selected: _filterCategory == null,
                    onSelected: (selected) {
                      setState(() {
                        _filterCategory = null;
                      });
                      _reloadData();
                    },
                    selectedColor: AppColors.rouge,
                    labelStyle: TextStyle(
                      color:
                          _filterCategory == null ? Colors.white : Colors.black,
                    ),
                  ),
                  const SizedBox(width: 8),
                  ...EquipmentCategory.values.map((category) {
                    return Padding(
                      padding: const EdgeInsets.only(right: 8),
                      child: ChoiceChip(
                        label: Text(_getCategoryLabel(category)),
                        selected: _filterCategory == category,
                        onSelected: (selected) {
                          setState(() {
                            _filterCategory = selected ? category : null;
                          });
                          _reloadData();
                        },
                        selectedColor: AppColors.rouge,
                        labelStyle: TextStyle(
                          color: _filterCategory == category
                              ? Colors.white
                              : Colors.black,
                        ),
                      ),
                    );
                  }),
                ],
              ),
            ),
            const SizedBox(height: 16),

            // Compteur de sélection
            Container(
              padding: const EdgeInsets.all(12),
              decoration: BoxDecoration(
                color: AppColors.rouge.withValues(alpha: 0.1),
                borderRadius: BorderRadius.circular(8),
              ),
              child: Row(
                children: [
                  const Icon(Icons.check_circle, color: AppColors.rouge),
                  const SizedBox(width: 8),
                  Text(
                    '${_tempSelectedIds.length} équipement(s) sélectionné(s)',
                    style: const TextStyle(fontWeight: FontWeight.bold),
                  ),
                ],
              ),
            ),
            const SizedBox(height: 16),

            // Liste des équipements
            Expanded(
              child: _paginatedEquipments.isEmpty && !_isLoadingMore
                  ? const Center(child: Text('Aucun équipement trouvé'))
                  : ListView.builder(
                      controller: _scrollController,
                      itemCount: _paginatedEquipments.length + (_isLoadingMore ? 1 : 0),
                      itemBuilder: (context, index) {
                        if (index == _paginatedEquipments.length) {
                          return const Center(
                            child: Padding(
                              padding: EdgeInsets.all(8.0),
                              child: CircularProgressIndicator(),
                            ),
                          );
                        }

                        final item = _paginatedEquipments[index];
                        final isSelected = _tempSelectedIds.contains(item.id);

                        return CheckboxListTile(
                          value: isSelected,
                          onChanged: (selected) {
                            setState(() {
                              if (selected == true) {
                                _tempSelectedIds.add(item.id);
                              } else {
                                _tempSelectedIds.remove(item.id);
                              }
                            });
                          },
                          title: Text(
                            item.id,
                            style: const TextStyle(fontWeight: FontWeight.bold),
                          ),
                          subtitle: Column(
                            crossAxisAlignment: CrossAxisAlignment.start,
                            children: [
                              if (item.brand != null || item.model != null)
                                Text('${item.brand ?? ''} ${item.model ?? ''}'),
                              const SizedBox(height: 4),
                              Text(
                                _getCategoryLabel(item.category),
                                style: TextStyle(
                                  fontSize: 12,
                                  color: Colors.grey.shade600,
                                ),
                              ),
                            ],
                          ),
                          secondary: Icon(
                            _getCategoryIcon(item.category),
                            color: AppColors.rouge,
                          ),
                          activeColor: AppColors.rouge,
                        );
                      },
                    ),
            ),

            // Boutons d'action
            const SizedBox(height: 16),
            Row(
              mainAxisAlignment: MainAxisAlignment.end,
              children: [
                TextButton(
                  onPressed: () {
                    // Ne pas appliquer les modifications, juste fermer
                    Navigator.pop(context);
                  },
                  child: const Text('Annuler'),
                ),
                const SizedBox(width: 16),
                ElevatedButton(
                  onPressed: () {
                    // Appliquer les modifications à l'original
                    widget.selectedIds.clear();
                    widget.selectedIds.addAll(_tempSelectedIds);
                    Navigator.pop(context);
                  },
                  style: ElevatedButton.styleFrom(
                    backgroundColor: AppColors.rouge,
                  ),
                  child: const Text(
                    'Valider',
                    style: TextStyle(color: Colors.white),
                  ),
                ),
              ],
            ),
          ],
        ),
      ),
    );
  }

  String _getCategoryLabel(EquipmentCategory category) {
    switch (category) {
      case EquipmentCategory.lighting:
        return 'Lumière';
      case EquipmentCategory.sound:
        return 'Son';
      case EquipmentCategory.video:
        return 'Vidéo';
      case EquipmentCategory.effect:
        return 'Effets';
      case EquipmentCategory.structure:
        return 'Structure';
      case EquipmentCategory.consumable:
        return 'Consommable';
      case EquipmentCategory.cable:
        return 'Câble';
      case EquipmentCategory.vehicle:
        return 'Véhicule';
      case EquipmentCategory.backline:
        return 'Régie / Backline';
      case EquipmentCategory.other:
        return 'Autre';
    }
  }

  IconData _getCategoryIcon(EquipmentCategory category) {
    switch (category) {
      case EquipmentCategory.lighting:
        return Icons.lightbulb;
      case EquipmentCategory.sound:
        return Icons.speaker;
      case EquipmentCategory.video:
        return Icons.videocam;
      case EquipmentCategory.effect:
        return Icons.auto_awesome;
      case EquipmentCategory.structure:
        return Icons.construction;
      case EquipmentCategory.consumable:
        return Icons.inventory;
      case EquipmentCategory.cable:
        return Icons.cable;
      case EquipmentCategory.vehicle:
        return Icons.local_shipping;
      case EquipmentCategory.backline:
        return Icons.piano;
      case EquipmentCategory.other:
        return Icons.category;
    }
  }
}
