export function timeAgo(iso: string): string {
  const diffMs = Date.now() - new Date(iso).getTime();
  const mins = Math.floor(diffMs / 60000);
  if (mins < 1) return "now";
  if (mins < 60) return `${mins}m`;
  const hours = Math.floor(mins / 60);
  if (hours < 24) return `${hours}h`;
  const days = Math.floor(hours / 24);
  if (days < 7) return `${days}d`;
  return new Date(iso).toLocaleDateString();
}

export function formatCount(n: number): string {
  if (n < 1000) return String(n);
  if (n < 1_000_000) {
    const k = n / 1000;
    // Rounding near the boundary (e.g. 999,999) can round up to "1000.0K" —
    // bump to millions formatting instead of ever showing 4-digit K values.
    if (Math.round(k * 10) / 10 >= 1000) {
      return (n / 1_000_000).toFixed(1) + "M";
    }
    return k.toFixed(n % 1000 >= 100 ? 1 : 0) + "K";
  }
  return (n / 1_000_000).toFixed(1) + "M";
}
