// Shared formatting utilities

/**
 * Format a timestamp as a relative time string (e.g., "2h ago", "5m ago")
 * @param timestamp - Timestamp in milliseconds
 */
export function formatTimeAgo(timestamp: number): string {
  const now = Date.now();
  const diff = now - timestamp;
  const minutes = Math.floor(diff / 60000);
  const hours = Math.floor(minutes / 60);
  const days = Math.floor(hours / 24);

  if (days > 0) return `${days}d ago`;
  if (hours > 0) return `${hours}h ago`;
  if (minutes > 0) return `${minutes}m ago`;
  return "just now";
}

/**
 * Format a Unix timestamp (seconds) as a relative time string.
 * Used for API responses that return Unix timestamps.
 * @param timestamp - Unix timestamp in seconds
 */
export function formatRelativeTime(timestamp: number): string {
  const now = Date.now() / 1000;
  const diff = now - timestamp;

  if (diff < 60) return "just now";
  if (diff < 3600) return `${Math.floor(diff / 60)}m ago`;
  if (diff < 86400) return `${Math.floor(diff / 3600)}h ago`;
  if (diff < 604800) return `${Math.floor(diff / 86400)}d ago`;
  return formatUnixTimestamp(timestamp);
}

/**
 * Format a Unix timestamp (seconds) as a date string.
 * @param timestamp - Unix timestamp in seconds
 * @param options - Intl.DateTimeFormatOptions for customization
 */
export function formatUnixTimestamp(
  timestamp: number,
  options?: Intl.DateTimeFormatOptions
): string {
  const defaultOptions: Intl.DateTimeFormatOptions = {
    month: "short",
    day: "numeric",
    year: "numeric",
  };
  return new Date(timestamp * 1000).toLocaleDateString("en-US", options ?? defaultOptions);
}

/**
 * Format a Unix timestamp (seconds) as a date-time string.
 * @param timestamp - Unix timestamp in seconds
 */
export function formatUnixDateTime(timestamp: number): string {
  return new Date(timestamp * 1000).toLocaleString("en-US", {
    month: "short",
    day: "numeric",
    year: "numeric",
    hour: "numeric",
    minute: "2-digit",
  });
}

/**
 * Format a timestamp as time (HH:MM)
 */
export function formatTime(timestamp: number): string {
  const date = new Date(timestamp);
  return date.toLocaleTimeString([], { hour: "2-digit", minute: "2-digit" });
}

/**
 * Format a timestamp as short date (Jan 15)
 */
export function formatDate(timestamp: number): string {
  const date = new Date(timestamp);
  return date.toLocaleDateString([], { month: "short", day: "numeric" });
}
