import 'package:intl/intl.dart';

class Formatters {
  Formatters._();

  static String number(num value) {
    return NumberFormat('#,##0').format(value);
  }

  static String decimal(num value, [int decimals = 2]) {
    return NumberFormat('#,##0.${'0' * decimals}').format(value);
  }

  static String currency(num value, [String symbol = '\$']) {
    return '$symbol${NumberFormat('#,##0.00').format(value)}';
  }

  static String percentage(num value) {
    return '${value.toStringAsFixed(1)}%';
  }

  static String date(String? isoDate) {
    if (isoDate == null || isoDate.isEmpty) return '-';
    try {
      final dt = DateTime.parse(isoDate);
      return DateFormat('MMM d, yyyy').format(dt);
    } catch (_) {
      return isoDate;
    }
  }

  static String dateTime(String? isoDate) {
    if (isoDate == null || isoDate.isEmpty) return '-';
    try {
      final dt = DateTime.parse(isoDate);
      return DateFormat('MMM d, yyyy · h:mm a').format(dt);
    } catch (_) {
      return isoDate;
    }
  }

  static String timeAgo(String? isoDate) {
    if (isoDate == null || isoDate.isEmpty) return '-';
    try {
      final dt = DateTime.parse(isoDate);
      final diff = DateTime.now().difference(dt);
      if (diff.inDays > 30) return DateFormat('MMM d').format(dt);
      if (diff.inDays > 0) return '${diff.inDays}d ago';
      if (diff.inHours > 0) return '${diff.inHours}h ago';
      if (diff.inMinutes > 0) return '${diff.inMinutes}m ago';
      return 'Just now';
    } catch (_) {
      return isoDate;
    }
  }

  static String countdown(int totalSeconds) {
    if (totalSeconds <= 0) return '0s';
    final h = totalSeconds ~/ 3600;
    final m = (totalSeconds % 3600) ~/ 60;
    final s = totalSeconds % 60;
    if (h > 0) return '${h}h ${m.toString().padLeft(2, '0')}m ${s.toString().padLeft(2, '0')}s';
    if (m > 0) return '${m}m ${s.toString().padLeft(2, '0')}s';
    return '${s}s';
  }
}