import 'package:flutter/material.dart';

import '../../../core/constants/design_system.dart';
import '../animated_widgets.dart';

/// Bouton d'action tout petit et réutilisable pour les posts sociaux.
///
/// Design compact et uniforme pour les actions (like, comment, share, etc.)
class SocialActionButton extends StatelessWidget {
  const SocialActionButton({
    required this.icon,
    required this.onTap,
    this.color,
    this.size = 22,
    this.padding,
    this.tooltip,
    super.key,
  });

  final IconData icon;
  final VoidCallback onTap;
  final Color? color;
  final double size;
  final EdgeInsets? padding;
  final String? tooltip;

  @override
  Widget build(BuildContext context) {
    final theme = Theme.of(context);
    final effectiveColor =
        color ?? theme.colorScheme.onSurface.withOpacity(0.7);

    final button = AnimatedScaleButton(
      onTap: onTap,
      scaleFactor: 0.85,
      child: Padding(
        padding: padding ??
            const EdgeInsets.all(DesignSystem.spacingSm),
        child: Icon(
          icon,
          size: size,
          color: effectiveColor,
        ),
      ),
    );

    if (tooltip != null) {
      return Tooltip(
        message: tooltip!,
        child: button,
      );
    }

    return button;
  }
}

/// Bouton d'action avec compteur pour les posts sociaux.
class SocialActionButtonWithCount extends StatelessWidget {
  const SocialActionButtonWithCount({
    required this.icon,
    required this.count,
    required this.onTap,
    this.color,
    this.activeColor,
    this.isActive = false,
    this.size = 22,
    this.showCount = true,
    super.key,
  });

  final IconData icon;
  final int count;
  final VoidCallback onTap;
  final Color? color;
  final Color? activeColor;
  final bool isActive;
  final double size;
  final bool showCount;

  @override
  Widget build(BuildContext context) {
    final theme = Theme.of(context);
    final effectiveColor = isActive
        ? (activeColor ?? theme.colorScheme.primary)
        : (color ?? theme.colorScheme.onSurface.withOpacity(0.7));

    return AnimatedScaleButton(
      onTap: onTap,
      scaleFactor: 0.85,
      child: Padding(
        padding: const EdgeInsets.all(DesignSystem.spacingSm),
        child: Row(
          mainAxisSize: MainAxisSize.min,
          children: [
            Icon(
              icon,
              size: size,
              color: effectiveColor,
            ),
            if (showCount && count > 0) ...[
              const SizedBox(width: 4),
              Text(
                _formatCount(count),
                style: theme.textTheme.bodySmall?.copyWith(
                  fontSize: 12,
                  fontWeight: FontWeight.w600,
                  color: effectiveColor,
                  letterSpacing: -0.2,
                ),
              ),
            ],
          ],
        ),
      ),
    );
  }

  String _formatCount(int count) {
    if (count >= 1000000) {
      final value = count / 1000000;
      return value % 1 == 0 ? '${value.toInt()}M' : '${value.toStringAsFixed(1)}M';
    } else if (count >= 1000) {
      final value = count / 1000;
      return value % 1 == 0 ? '${value.toInt()}K' : '${value.toStringAsFixed(1)}K';
    }
    return count.toString();
  }
}
