/**
 * Shared utility functions for the Calendink dashboard.
 * Import from here — never duplicate these across components.
 */

/** Format uptime seconds into human-readable string */
export function formatUptime(seconds) {
  const d = Math.floor(seconds / 86400);
  const h = Math.floor((seconds % 86400) / 3600);
  const m = Math.floor((seconds % 3600) / 60);
  const s = seconds % 60;
  const parts = [];
  if (d > 0) parts.push(`${d}d`);
  if (h > 0) parts.push(`${h}h`);
  if (m > 0) parts.push(`${m}m`);
  parts.push(`${s}s`);
  return parts.join(' ');
}

/** Format bytes into human-readable string */
export function formatBytes(bytes) {
  if (bytes >= 1048576) return `${(bytes / 1048576).toFixed(1)} MB`;
  if (bytes >= 1024) return `${(bytes / 1024).toFixed(1)} KB`;
  return `${bytes} B`;
}

/** Format a Unix timestamp (seconds) as a human-readable relative string */
export function formatRelativeDate(timestamp) {
  const now = Date.now() / 1000;
  const diff = timestamp - now;
  const absDiff = Math.abs(diff);

  if (absDiff < 3600) {
    const mins = Math.round(absDiff / 60);
    return diff < 0 ? `${mins}m ago` : `in ${mins}m`;
  }
  if (absDiff < 86400) {
    const hours = Math.round(absDiff / 3600);
    return diff < 0 ? `${hours}h ago` : `in ${hours}h`;
  }
  const days = Math.round(absDiff / 86400);
  return diff < 0 ? `${days}d ago` : `in ${days}d`;
}

/** Returns true if a Unix timestamp (seconds) is in the past */
export function isOverdue(timestamp) {
  return timestamp < Date.now() / 1000;
}
