import 'package:flutter/material.dart';

import '../../domain/entities/friend_request.dart';

/// Widget pour afficher une demande d'amitié en attente.
/// Permet d'accepter ou de rejeter la demande (reçue) ou d'annuler (envoyée).
class FriendRequestCard extends StatelessWidget {
  const FriendRequestCard({
    required this.request,
    required this.onAccept,
    required this.onReject,
    this.isSentRequest = false,
    super.key,
  });

  final FriendRequest request;
  final VoidCallback? onAccept;
  final VoidCallback onReject;
  final bool isSentRequest;

  @override
  Widget build(BuildContext context) {
    final theme = Theme.of(context);
    final displayName = isSentRequest
        ? (request.friendFullName.isNotEmpty
            ? request.friendFullName
            : 'Utilisateur inconnu')
        : (request.userFullName.isNotEmpty
            ? request.userFullName
            : 'Utilisateur inconnu');
    final initial = displayName.isNotEmpty ? displayName[0].toUpperCase() : '?';

    return Card(
      margin: const EdgeInsets.symmetric(horizontal: 16, vertical: 6),
      elevation: 2,
      shape: RoundedRectangleBorder(
        borderRadius: BorderRadius.circular(12),
      ),
      child: Padding(
        padding: const EdgeInsets.all(12),
        child: Row(
          children: [
            // Avatar
            CircleAvatar(
              radius: 24,
              backgroundColor: theme.colorScheme.primaryContainer,
              child: Text(
                initial,
                style: TextStyle(
                  color: theme.colorScheme.onPrimaryContainer,
                  fontSize: 18,
                  fontWeight: FontWeight.bold,
                ),
              ),
            ),
            const SizedBox(width: 12),
            // Informations
            Expanded(
              child: Column(
                crossAxisAlignment: CrossAxisAlignment.start,
                mainAxisSize: MainAxisSize.min,
                children: [
                  Text(
                    displayName,
                    style: theme.textTheme.titleMedium?.copyWith(
                      fontWeight: FontWeight.w600,
                    ),
                    maxLines: 1,
                    overflow: TextOverflow.ellipsis,
                  ),
                  const SizedBox(height: 2),
                  Text(
                    isSentRequest ? 'Demande envoyée' : 'Demande d\'amitié',
                    style: theme.textTheme.bodySmall?.copyWith(
                      color: theme.colorScheme.onSurface.withOpacity(0.6),
                      fontSize: 12,
                    ),
                  ),
                ],
              ),
            ),
            // Boutons d'action
            if (isSentRequest)
              _buildCancelButton(theme)
            else
              _buildActionButtons(theme),
          ],
        ),
      ),
    );
  }

  /// Construit le bouton d'annulation pour les demandes envoyées.
  Widget _buildCancelButton(ThemeData theme) {
    return Material(
      color: Colors.transparent,
      child: InkWell(
        onTap: onReject,
        borderRadius: BorderRadius.circular(20),
        child: Container(
          padding: const EdgeInsets.all(8),
          decoration: BoxDecoration(
            color: Colors.orange.withOpacity(0.1),
            borderRadius: BorderRadius.circular(20),
          ),
          child: const Icon(
            Icons.close,
            color: Colors.orange,
            size: 20,
          ),
        ),
      ),
    );
  }

  /// Construit les boutons d'action pour les demandes reçues.
  Widget _buildActionButtons(ThemeData theme) {
    return Row(
      mainAxisSize: MainAxisSize.min,
      children: [
        // Bouton Accepter
        Material(
          color: Colors.transparent,
          child: InkWell(
            onTap: onAccept,
            borderRadius: BorderRadius.circular(20),
            child: Container(
              padding: const EdgeInsets.all(8),
              decoration: BoxDecoration(
                color: Colors.green.withOpacity(0.1),
                borderRadius: BorderRadius.circular(20),
              ),
              child: const Icon(
                Icons.check_circle,
                color: Colors.green,
                size: 20,
              ),
            ),
          ),
        ),
        const SizedBox(width: 8),
        // Bouton Rejeter
        Material(
          color: Colors.transparent,
          child: InkWell(
            onTap: onReject,
            borderRadius: BorderRadius.circular(20),
            child: Container(
              padding: const EdgeInsets.all(8),
              decoration: BoxDecoration(
                color: Colors.red.withOpacity(0.1),
                borderRadius: BorderRadius.circular(20),
              ),
              child: const Icon(
                Icons.cancel,
                color: Colors.red,
                size: 20,
              ),
            ),
          ),
        ),
      ],
    );
  }
}

