import 'package:flutter/material.dart';
import 'package:p_tits_pas/models/user.dart';
import 'package:p_tits_pas/services/user_service.dart';
import 'package:p_tits_pas/widgets/admin/gestionnaire_card.dart';

class GestionnaireManagementWidget extends StatefulWidget {
  const GestionnaireManagementWidget({Key? key}) : super(key: key);

  @override
  State<GestionnaireManagementWidget> createState() =>
      _GestionnaireManagementWidgetState();
}

class _GestionnaireManagementWidgetState
    extends State<GestionnaireManagementWidget> {
  bool _isLoading = false;
  String? _error;
  List<AppUser> _gestionnaires = [];
  List<AppUser> _filteredGestionnaires = [];
  final TextEditingController _searchController = TextEditingController();

  @override
  void initState() {
    super.initState();
    _loadGestionnaires();
    _searchController.addListener(_onSearchChanged);
  }

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

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

  void _onSearchChanged() {
    final query = _searchController.text.toLowerCase();
    setState(() {
      _filteredGestionnaires = _gestionnaires.where((u) {
        final name = u.fullName.toLowerCase();
        final email = u.email.toLowerCase();
        return name.contains(query) || email.contains(query);
      }).toList();
    });
  }

  @override
  Widget build(BuildContext context) {
    return Padding(
      padding: const EdgeInsets.all(16),
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.stretch,
        children: [
          // 🔹 Barre du haut avec bouton
          Row(
            children: [
              Expanded(
                child: TextField(
                  controller: _searchController,
                  decoration: const InputDecoration(
                    hintText: "Rechercher un gestionnaire...",
                    prefixIcon: Icon(Icons.search),
                    border: OutlineInputBorder(),
                  ),
                ),
              ),
              const SizedBox(width: 16),
              ElevatedButton.icon(
                onPressed: () {
                  // TODO: Rediriger vers la page de création
                },
                icon: const Icon(Icons.add),
                label: const Text("Créer un gestionnaire"),
              ),
            ],
          ),
          const SizedBox(height: 24),

          // 🔹 Liste des gestionnaires
          if (_isLoading)
            const Center(child: CircularProgressIndicator())
          else if (_error != null)
            Center(child: Text('Erreur: $_error', style: const TextStyle(color: Colors.red)))
          else if (_filteredGestionnaires.isEmpty)
            const Center(child: Text("Aucun gestionnaire trouvé."))
          else
            Expanded(
              child: ListView.builder(
                itemCount: _filteredGestionnaires.length,
                itemBuilder: (context, index) {
                  final user = _filteredGestionnaires[index];
                  return GestionnaireCard(
                    name: user.fullName.isNotEmpty ? user.fullName : "Sans nom",
                    email: user.email,
                  );
                },
              ),
            )
        ],
      ),
    );
  }
}
