import 'package:flutter/material.dart';
import 'package:p_tits_pas/models/assistante_maternelle_model.dart';
import 'package:p_tits_pas/utils/am_vigilance.dart';
import 'package:p_tits_pas/services/user_service.dart';
import 'package:p_tits_pas/widgets/admin/common/admin_am_edit_modal.dart';
import 'package:p_tits_pas/widgets/admin/common/admin_user_card.dart';
import 'package:p_tits_pas/widgets/admin/common/user_list.dart';

class AssistanteMaternelleManagementWidget extends StatefulWidget {
  final String searchQuery;
  final int? capacityMin;

  const AssistanteMaternelleManagementWidget({
    super.key,
    required this.searchQuery,
    this.capacityMin,
  });

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

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

  @override
  void initState() {
    super.initState();
    _loadAssistantes();
  }

  @override
  void dispose() => super.dispose();

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

  @override
  Widget build(BuildContext context) {
    final query = widget.searchQuery.toLowerCase();
    final filteredAssistantes = _assistantes.where((am) {
      final matchesName = am.user.fullName.toLowerCase().contains(query) ||
          am.user.email.toLowerCase().contains(query) ||
          (am.residenceCity?.toLowerCase().contains(query) ?? false);
      final matchesCapacity = widget.capacityMin == null ||
          (am.maxChildren != null && am.maxChildren! >= widget.capacityMin!);
      return matchesName && matchesCapacity;
    }).toList();

    return UserList(
      isLoading: _isLoading,
      error: _error,
      isEmpty: filteredAssistantes.isEmpty,
      emptyMessage: 'Aucune assistante maternelle trouvée.',
      itemCount: filteredAssistantes.length,
      itemBuilder: (context, index) {
        final assistante = filteredAssistantes[index];
        final vigilance = amPlacesVigilanceMessage(assistante);
        return AdminUserCard(
          title: assistante.user.fullName,
          avatarUrl: assistante.user.photoUrl,
          fallbackIcon: Icons.face,
          vigilanceTooltip: vigilance,
          onCardTap: () => _openAssistanteDetails(assistante),
          subtitleLines: [
            assistante.user.email,
            'Zone : ${assistante.residenceCity ?? 'N/A'} | Capacité : ${assistante.maxChildren ?? 0}',
          ],
          actions: [
            IconButton(
              icon: const Icon(Icons.edit),
              tooltip: 'Modifier',
              onPressed: () {
                _openAssistanteDetails(assistante);
              },
            ),
          ],
        );
      },
    );
  }

  void _openAssistanteDetails(AssistanteMaternelleModel assistante) {
    showDialog<void>(
      context: context,
      builder: (context) => AdminAmEditModal(
        assistante: assistante,
        onSaved: _loadAssistantes,
      ),
    );
  }
}
