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

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

  @override
  State<AdminManagementWidget> createState() => _AdminManagementWidgetState();
}

class _AdminManagementWidgetState extends State<AdminManagementWidget> {
  bool _isLoading = false;
  String? _error;
  List<AppUser> _admins = [];
  List<AppUser> _filteredAdmins = [];
  final TextEditingController _searchController = TextEditingController();

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

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

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

  void _onSearchChanged() {
    final query = _searchController.text.toLowerCase();
    setState(() {
      _filteredAdmins = _admins.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: [
          Row(
            children: [
              Expanded(
                child: TextField(
                  controller: _searchController,
                  decoration: const InputDecoration(
                    hintText: "Rechercher un administrateur...",
                    prefixIcon: Icon(Icons.search),
                    border: OutlineInputBorder(),
                  ),
                ),
              ),
              const SizedBox(width: 16),
              ElevatedButton.icon(
                onPressed: () {
                  // TODO: Créer admin
                },
                icon: const Icon(Icons.add),
                label: const Text("Créer un admin"),
              ),
            ],
          ),
          const SizedBox(height: 24),
          if (_isLoading)
            const Center(child: CircularProgressIndicator())
          else if (_error != null)
            Center(child: Text('Erreur: $_error', style: const TextStyle(color: Colors.red)))
          else if (_filteredAdmins.isEmpty)
            const Center(child: Text("Aucun administrateur trouvé."))
          else
            Expanded(
              child: ListView.builder(
                itemCount: _filteredAdmins.length,
                itemBuilder: (context, index) {
                  final user = _filteredAdmins[index];
                  return Card(
                    margin: const EdgeInsets.only(bottom: 12),
                    child: ListTile(
                      leading: CircleAvatar(
                        child: Text(user.fullName.isNotEmpty
                            ? user.fullName[0].toUpperCase()
                            : 'A'),
                      ),
                      title: Text(user.fullName.isNotEmpty
                          ? user.fullName
                          : 'Sans nom'),
                      subtitle: Text(user.email),
                      trailing: Row(
                        mainAxisSize: MainAxisSize.min,
                        children: [
                          IconButton(
                            icon: const Icon(Icons.edit),
                            onPressed: () {},
                          ),
                          IconButton(
                            icon: const Icon(Icons.delete),
                            onPressed: () {},
                          ),
                        ],
                      ),
                    ),
                  );
                },
              ),
            )
        ],
      ),
    );
  }
}
