import 'package:flutter/material.dart';
import 'package:afterwork/core/utils/date_formatter.dart';
import 'event_menu.dart';

class EventHeader extends StatelessWidget {
  final String userName;
  final String userLastName;
  final String? eventDate;
  final String? imageUrl;
  final String location;
  final GlobalKey menuKey;
  final BuildContext menuContext;
  final VoidCallback onClose; // Ajout d'un callback pour l'action de fermeture

  const EventHeader({
    Key? key,
    required this.userName,
    required this.userLastName,
    this.eventDate,
    this.imageUrl,
    required this.location,
    required this.menuKey,
    required this.menuContext,
    required this.onClose, // Initialisation du callback de fermeture
  }) : super(key: key);

  @override
  Widget build(BuildContext context) {
    DateTime? date;
    try {
      date = DateTime.parse(eventDate ?? '');
    } catch (e) {
      date = null;
    }
    String formattedDate = date != null ? DateFormatter.formatDate(date) : 'Date inconnue';

    return Stack(
      children: [
        Row(
          children: [
            CircleAvatar(
              backgroundColor: Colors.grey.shade800,
              backgroundImage: imageUrl != null && imageUrl!.isNotEmpty
                  ? NetworkImage(imageUrl!)
                  : const AssetImage('lib/assets/images/placeholder.png') as ImageProvider,
              radius: 22,
            ),
            const SizedBox(width: 8),
            Expanded(
              child: Column(
                crossAxisAlignment: CrossAxisAlignment.start,
                children: [
                  Text(
                    '$userName $userLastName',
                    style: const TextStyle(
                      color: Colors.white,
                      fontSize: 14,
                      fontWeight: FontWeight.w500,
                    ),
                    maxLines: 1,
                    overflow: TextOverflow.ellipsis,
                  ),
                  const SizedBox(height: 4),
                  Text(
                    formattedDate,
                    style: const TextStyle(
                      color: Colors.white54,
                      fontSize: 12,
                    ),
                  ),
                  const SizedBox(height: 4),
                  Row(
                    children: [
                      Expanded(
                        child: Text(
                          location.isNotEmpty ? location : 'Lieu non spécifié',
                          style: const TextStyle(
                            color: Colors.white60,
                            fontSize: 12,
                            fontStyle: FontStyle.italic,
                          ),
                          maxLines: 1,
                          overflow: TextOverflow.ellipsis,
                        ),
                      ),
                      const SizedBox(width: 8),
                    ],
                  ),
                ],
              ),
            ),
          ],
        ),
        // Ajout des boutons dans le coin supérieur droit
        Positioned(
          top: 0,
          right: 0,
          child: Row(
            children: [
              IconButton(
                key: menuKey,
                icon: const Icon(Icons.more_vert, color: Colors.white54, size: 20),
                splashRadius: 20,
                onPressed: () {
                  showEventOptions(menuContext, menuKey);
                },
              ),
              IconButton(
                icon: const Icon(Icons.close, color: Colors.white54, size: 20),
                splashRadius: 20,
                onPressed: onClose, // Appel du callback de fermeture
              ),
            ],
          ),
        ),
      ],
    );
  }
}
