import 'package:flutter/material.dart';
import 'package:p_tits_pas/models/user.dart';
import 'package:p_tits_pas/utils/phone_utils.dart';
import 'package:p_tits_pas/screens/administrateurs/creation/gestionnaires_create.dart';
import 'package:p_tits_pas/services/auth_service.dart';
import 'package:p_tits_pas/services/user_service.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 AdminManagementWidget extends StatefulWidget {
  final String searchQuery;

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

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

class _AdminManagementWidgetState extends State<AdminManagementWidget> {
  bool _isLoading = false;
  String? _error;
  List<AppUser> _admins = [];
  String? _currentUserRole;

  @override
  void initState() {
    super.initState();
    _loadCurrentUserRole();
    _loadAdmins();
  }

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

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

  Future<void> _loadCurrentUserRole() async {
    final cached = await AuthService.getCurrentUser();
    if (!mounted) return;
    if (cached != null) {
      setState(() {
        _currentUserRole = (cached.role).toLowerCase();
      });
      return;
    }
    final refreshed = await AuthService.refreshCurrentUser();
    if (!mounted || refreshed == null) return;
    setState(() {
      _currentUserRole = (refreshed.role).toLowerCase();
    });
  }

  bool _isSuperAdmin(AppUser user) => (user.role).toLowerCase() == 'super_admin';

  bool _canEditAdmin(AppUser target) {
    if (!_isSuperAdmin(target)) return true;
    return _currentUserRole == 'super_admin';
  }

  Future<void> _openAdminEditDialog(AppUser user) async {
    final canEdit = _canEditAdmin(user);
    final changed = await showDialog<bool>(
      context: context,
      barrierDismissible: false,
      builder: (dialogContext) {
        return AdminUserFormDialog(
          initialUser: user,
          adminMode: true,
          withRelais: false,
          readOnly: !canEdit,
        );
      },
    );
    if (changed == true && canEdit) {
      await _loadAdmins();
    }
  }

  @override
  Widget build(BuildContext context) {
    final query = widget.searchQuery.toLowerCase();
    final filteredAdmins = _admins.where((u) {
      final name = u.fullName.toLowerCase();
      final email = u.email.toLowerCase();
      return name.contains(query) || email.contains(query);
    }).toList();

    return UserList(
      isLoading: _isLoading,
      error: _error,
      isEmpty: filteredAdmins.isEmpty,
      emptyMessage: 'Aucun administrateur trouvé.',
      itemCount: filteredAdmins.length,
      itemBuilder: (context, index) {
        final user = filteredAdmins[index];
        final isSuperAdmin = _isSuperAdmin(user);
        final canEdit = _canEditAdmin(user);
        return AdminUserCard(
          title: user.fullName,
          fallbackIcon: isSuperAdmin
              ? Icons.verified_user_outlined
              : Icons.manage_accounts_outlined,
          onCardTap: () => _openAdminEditDialog(user),
          subtitleLines: [
            user.email,
            'Téléphone : ${user.telephone?.trim().isNotEmpty == true ? formatPhoneForDisplay(user.telephone!) : 'Non renseigné'}',
          ],
          avatarUrl: user.photoUrl,
          borderColor: isSuperAdmin
              ? const Color(0xFF8E6AC8)
              : Colors.grey.shade300,
          backgroundColor: isSuperAdmin
              ? const Color(0xFFF4EEFF)
              : Colors.white,
          titleColor: isSuperAdmin ? const Color(0xFF5D2F99) : null,
          infoColor: isSuperAdmin
              ? const Color(0xFF6D4EA1)
              : Colors.black54,
          actions: [
            IconButton(
              icon: Icon(
                canEdit ? Icons.edit_outlined : Icons.visibility_outlined,
              ),
              tooltip: canEdit ? 'Modifier' : 'Consulter',
              onPressed: () {
                _openAdminEditDialog(user);
              },
            ),
          ],
        );
      },
    );
  }
}
