import 'package:flutter/material.dart';
import 'package:cached_network_image/cached_network_image.dart';
import '../design_system/tokens/app_colors.dart';
import '../design_system/tokens/app_typography.dart';
import '../design_system/tokens/color_tokens.dart';

/// UnionFlow Mobile - Composant DRY : MiniAvatar
/// Évite toute répétition de configuration d'image de profil.
/// Formats contraints (24px, 32px max).
class MiniAvatar extends StatelessWidget {
  final String? imageUrl;
  final String fallbackText; // Ex: "JD" pour John Doe
  final double size;
  final bool isOnline; // Ajoute une petite pastille verte
  final Color? backgroundColor;
  final Color? iconColor;
  final bool isIcon;

  const MiniAvatar({
    Key? key,
    this.imageUrl,
    required this.fallbackText,
    this.size = 32.0,
    this.isOnline = false,
    this.backgroundColor,
    this.iconColor,
    this.isIcon = false,
  }) : super(key: key);

  @override
  Widget build(BuildContext context) {
    final isDark = Theme.of(context).brightness == Brightness.dark;
    final borderColor = isDark ? AppColors.borderDark : AppColors.border;

    return Stack(
      children: [
        Container(
          width: size,
          height: size,
          decoration: BoxDecoration(
            shape: BoxShape.circle,
            color: backgroundColor ?? AppColors.primary.withOpacity(isDark ? 0.2 : 0.1),
            border: Border.all(color: borderColor, width: 0.5),
          ),
          child: ClipOval(
            child: isIcon 
                ? _buildIcon()
                : (imageUrl != null && imageUrl!.isNotEmpty
                    ? CachedNetworkImage(
                        imageUrl: imageUrl!,
                        fit: BoxFit.cover,
                        placeholder: (context, url) => _buildFallback(),
                        errorWidget: (context, url, error) => _buildFallback(),
                      )
                    : _buildFallback()),
          ),
        ),
        if (isOnline)
          Positioned(
            bottom: 0,
            right: 0,
            child: Container(
              width: size * 0.3,
              height: size * 0.3,
              decoration: BoxDecoration(
                color: AppColors.success,
                shape: BoxShape.circle,
                border: Border.all(
                  color: Theme.of(context).scaffoldBackgroundColor,
                  width: 1.5,
                ),
              ),
            ),
          ),
      ],
    );
  }

  Widget _buildFallback() {
    return Center(
      child: Text(
        fallbackText.toUpperCase(),
        style: AppTypography.actionText.copyWith(
          color: iconColor ?? AppColors.primary,
          fontSize: size * 0.4,
        ),
      ),
    );
  }

  Widget _buildIcon() {
    IconData iconData;
    switch (fallbackText) {
      case 'people': iconData = Icons.people; break;
      case 'event': iconData = Icons.event; break;
      case 'business': iconData = Icons.business; break;
      case 'settings': iconData = Icons.settings; break;
      default: iconData = Icons.notifications;
    }
    return Center(
      child: Icon(
        iconData,
        color: iconColor ?? AppColors.primary,
        size: size * 0.6,
      ),
    );
  }
}
