/// Widget tuile de conversation
library conversation_tile;

import 'package:flutter/material.dart';
import 'package:intl/intl.dart';
import '../../../../shared/design_system/unionflow_design_system.dart';
import '../../domain/entities/conversation.dart';

class ConversationTile extends StatelessWidget {
  final Conversation conversation;
  final VoidCallback onTap;

  const ConversationTile({
    super.key,
    required this.conversation,
    required this.onTap,
  });

  String _formatDate(DateTime date) {
    final now = DateTime.now();
    final difference = now.difference(date);

    if (difference.inDays == 0) {
      return DateFormat('HH:mm').format(date);
    } else if (difference.inDays == 1) {
      return 'Hier';
    } else if (difference.inDays < 7) {
      return DateFormat('EEEE', 'fr_FR').format(date);
    } else {
      return DateFormat('dd/MM/yy').format(date);
    }
  }

  @override
  Widget build(BuildContext context) {
    return InkWell(
      onTap: onTap,
      borderRadius: BorderRadius.circular(SpacingTokens.radiusMd),
      child: Container(
        padding: const EdgeInsets.all(SpacingTokens.md),
        decoration: BoxDecoration(
          color: ColorTokens.surface,
          borderRadius: BorderRadius.circular(SpacingTokens.radiusMd),
          border: Border.all(
            color: conversation.hasUnread
                ? AppColors.primaryGreen.withOpacity(0.3)
                : ColorTokens.outline,
          ),
        ),
        child: Row(
          children: [
            // Avatar
            CircleAvatar(
              radius: 24,
              backgroundColor: AppColors.primaryGreen.withOpacity(0.1),
              backgroundImage: conversation.avatarUrl != null
                  ? NetworkImage(conversation.avatarUrl!)
                  : null,
              child: conversation.avatarUrl == null
                  ? Text(
                      conversation.name.isNotEmpty
                          ? conversation.name[0].toUpperCase()
                          : '?',
                      style: AppTypography.actionText.copyWith(
                        color: AppColors.primaryGreen,
                      ),
                    )
                  : null,
            ),

            const SizedBox(width: SpacingTokens.md),

            // Contenu
            Expanded(
              child: Column(
                crossAxisAlignment: CrossAxisAlignment.start,
                children: [
                  Row(
                    children: [
                      Expanded(
                        child: Text(
                          conversation.name,
                          style: AppTypography.actionText.copyWith(
                            fontWeight: conversation.hasUnread
                                ? FontWeight.bold
                                : FontWeight.normal,
                          ),
                          maxLines: 1,
                          overflow: TextOverflow.ellipsis,
                        ),
                      ),
                      if (conversation.lastMessage != null)
                        Text(
                          _formatDate(conversation.lastMessage!.createdAt),
                          style: AppTypography.subtitleSmall.copyWith(
                            color: AppColors.textSecondaryLight,
                          ),
                        ),
                    ],
                  ),
                  if (conversation.lastMessage != null) ...[
                    const SizedBox(height: 4),
                    Text(
                      conversation.lastMessage!.content,
                      style: AppTypography.bodyTextSmall.copyWith(
                        color: AppColors.textSecondaryLight,
                        fontWeight: conversation.hasUnread
                            ? FontWeight.w600
                            : FontWeight.normal,
                      ),
                      maxLines: 2,
                      overflow: TextOverflow.ellipsis,
                    ),
                  ],
                ],
              ),
            ),

            // Badge non lus
            if (conversation.hasUnread) ...[
              const SizedBox(width: SpacingTokens.sm),
              Container(
                padding: const EdgeInsets.symmetric(
                  horizontal: 8,
                  vertical: 4,
                ),
                decoration: BoxDecoration(
                  color: AppColors.primaryGreen,
                  borderRadius: BorderRadius.circular(SpacingTokens.radiusCircular),
                ),
                child: Text(
                  '${conversation.unreadCount}',
                  style: AppTypography.badgeText.copyWith(
                    color: Colors.white,
                    fontWeight: FontWeight.bold,
                  ),
                ),
              ),
            ],

            // Icônes statut
            if (conversation.isPinned || conversation.isMuted) ...[
              const SizedBox(width: SpacingTokens.sm),
              Column(
                children: [
                  if (conversation.isPinned)
                    Icon(
                      Icons.push_pin,
                      size: 16,
                      color: AppColors.textSecondaryLight,
                    ),
                  if (conversation.isMuted)
                    Icon(
                      Icons.volume_off,
                      size: 16,
                      color: AppColors.textSecondaryLight,
                    ),
                ],
              ),
            ],
          ],
        ),
      ),
    );
  }
}
