import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import 'package:em2rp/models/user_model.dart';
import 'package:em2rp/models/event_model.dart';
import 'package:em2rp/providers/event_provider.dart';
import 'package:em2rp/views/widgets/user_management/user_multi_select_widget.dart';

class EventDetailsEquipe extends StatelessWidget {
  final EventModel event;

  const EventDetailsEquipe({
    super.key,
    required this.event,
  });

  @override
  Widget build(BuildContext context) {
    if (event.workforce.isEmpty) {
      return Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          Text(
            'Equipe',
            style: Theme.of(context).textTheme.titleLarge?.copyWith(
                  color: Colors.black,
                  fontWeight: FontWeight.bold,
                ),
          ),
          const SizedBox(height: 8),
          Text(
            'Aucun membre assigné.',
            style: Theme.of(context).textTheme.bodyMedium,
          ),
        ],
      );
    }

    // Récupérer les utilisateurs depuis le cache du provider
    final eventProvider = Provider.of<EventProvider>(context, listen: false);
    final workforceUsers = eventProvider.getWorkforceUsers(event);

    // Convertir en UserModel
    final users = workforceUsers.map((userData) {
      return UserModel(
        uid: userData['uid'] ?? '',
        firstName: userData['firstName'] ?? '',
        lastName: userData['lastName'] ?? '',
        email: userData['email'] ?? '',
        phoneNumber: userData['phoneNumber'] ?? '',
        profilePhotoUrl: userData['profilePhotoUrl'] ?? '',
        role: '', // Pas besoin du rôle pour l'affichage
      );
    }).toList();

    return Column(
      crossAxisAlignment: CrossAxisAlignment.start,
      children: [
        Text(
          'Equipe',
          style: Theme.of(context).textTheme.titleLarge?.copyWith(
                color: Colors.black,
                fontWeight: FontWeight.bold,
              ),
        ),
        const SizedBox(height: 8),
        if (users.isEmpty)
          Text(
            'Aucun membre assigné.',
            style: Theme.of(context).textTheme.bodyMedium?.copyWith(
                  color: Colors.orange[700],
                ),
          ),
        if (users.isNotEmpty)
          UserChipsList(
            users: users,
            showRemove: false,
          ),
      ],
    );
  }
}

