import 'package:flutter/material.dart';
import 'package:p_tits_pas/models/assistante_maternelle_model.dart';
import 'package:p_tits_pas/services/user_service.dart';

class AssistanteMaternelleManagementWidget extends StatefulWidget {
  const AssistanteMaternelleManagementWidget({super.key});

  @override
  State<AssistanteMaternelleManagementWidget> createState() =>
      _AssistanteMaternelleManagementWidgetState();
}

class _AssistanteMaternelleManagementWidgetState
    extends State<AssistanteMaternelleManagementWidget> {
  bool _isLoading = false;
  String? _error;
  List<AssistanteMaternelleModel> _assistantes = [];
  List<AssistanteMaternelleModel> _filteredAssistantes = [];

  final TextEditingController _zoneController = TextEditingController();
  final TextEditingController _capacityController = TextEditingController();

  @override
  void initState() {
    super.initState();
    _loadAssistantes();
    _zoneController.addListener(_filter);
    _capacityController.addListener(_filter);
  }

  @override
  void dispose() {
    _zoneController.dispose();
    _capacityController.dispose();
    super.dispose();
  }

  Future<void> _loadAssistantes() async {
    setState(() {
      _isLoading = true;
      _error = null;
    });
    try {
      final list = await UserService.getAssistantesMaternelles();
      if (!mounted) return;
      setState(() {
        _assistantes = list;
        _filter();
        _isLoading = false;
      });
    } catch (e) {
      if (!mounted) return;
      setState(() {
        _error = e.toString();
        _isLoading = false;
      });
    }
  }

  void _filter() {
    final zoneQuery = _zoneController.text.toLowerCase();
    final capacityQuery = int.tryParse(_capacityController.text);

    setState(() {
      _filteredAssistantes = _assistantes.where((am) {
        final matchesZone = zoneQuery.isEmpty ||
            (am.residenceCity?.toLowerCase().contains(zoneQuery) ?? false);
        final matchesCapacity = capacityQuery == null ||
            (am.maxChildren != null && am.maxChildren! >= capacityQuery);
        return matchesZone && matchesCapacity;
      }).toList();
    });
  }

  @override
  Widget build(BuildContext context) {
    return Padding(
      padding: const EdgeInsets.all(16),
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          // 🔎 Zone de filtre
          _buildFilterSection(),

          const SizedBox(height: 16),

          // 📋 Liste des assistantes
          if (_isLoading)
            const Center(child: CircularProgressIndicator())
          else if (_error != null)
            Center(child: Text('Erreur: $_error', style: const TextStyle(color: Colors.red)))
          else if (_filteredAssistantes.isEmpty)
            const Center(child: Text("Aucune assistante maternelle trouvée."))
          else
            Expanded(
              child: ListView.builder(
                itemCount: _filteredAssistantes.length,
                itemBuilder: (context, index) {
                  final assistante = _filteredAssistantes[index];
                  return Card(
                    margin: const EdgeInsets.symmetric(vertical: 8),
                    child: ListTile(
                      leading: CircleAvatar(
                        backgroundImage: assistante.user.photoUrl != null
                            ? NetworkImage(assistante.user.photoUrl!)
                            : null,
                        child: assistante.user.photoUrl == null
                            ? const Icon(Icons.face)
                            : null,
                      ),
                      title: Text(assistante.user.fullName.isNotEmpty
                          ? assistante.user.fullName
                          : 'Sans nom'),
                      subtitle: Text(
                          "N° Agrément : ${assistante.approvalNumber ?? 'N/A'}\nZone : ${assistante.residenceCity ?? 'N/A'} | Capacité : ${assistante.maxChildren ?? 0}"),
                      trailing: Row(
                        mainAxisSize: MainAxisSize.min,
                        children: [
                          IconButton(
                            icon: const Icon(Icons.edit),
                            onPressed: () {
                              // TODO: Ajouter modification
                            },
                          ),
                          IconButton(
                            icon: const Icon(Icons.delete),
                            onPressed: () {
                              // TODO: Ajouter suppression
                            },
                          ),
                        ],
                      ),
                    ),
                  );
                },
              ),
            ),
        ],
      ),
    );
  }

  Widget _buildFilterSection() {
    return Wrap(
      spacing: 16,
      runSpacing: 8,
      children: [
        SizedBox(
          width: 200,
          child: TextField(
            controller: _zoneController,
            decoration: const InputDecoration(
              labelText: "Zone géographique",
              border: OutlineInputBorder(),
              prefixIcon: Icon(Icons.location_on),
            ),
          ),
        ),
        SizedBox(
          width: 200,
          child: TextField(
            controller: _capacityController,
            decoration: const InputDecoration(
              labelText: "Capacité minimum",
              border: OutlineInputBorder(),
            ),
            keyboardType: TextInputType.number,
          ),
        ),
      ],
    );
  }
}
