/**
 * Small display formatters shared across the UI. Byte sizes reuse
 * `formatSize` from fsTree.js; relative time lives here.
 */

const MINUTE = 60 * 1000;
const HOUR = 60 * MINUTE;
const DAY = 24 * HOUR;
const WEEK = 7 * DAY;
const MONTH = 30 * DAY;
const YEAR = 365 * DAY;

/**
 * Human "3 days ago" style label for a timestamp (ISO string, epoch ms, or
 * Date). Returns "never" when nothing is passed — used for a project's last
 * commit, which may be null.
 */
export function formatRelativeTime(value) {
  if (value == null) return 'never';
  const then = value instanceof Date ? value.getTime() : new Date(value).getTime();
  if (Number.isNaN(then)) return 'never';

  const diff = Date.now() - then;
  if (diff < MINUTE) return 'just now';

  const plural = (n, unit) => `${n} ${unit}${n === 1 ? '' : 's'} ago`;
  if (diff < HOUR) return plural(Math.floor(diff / MINUTE), 'minute');
  if (diff < DAY) return plural(Math.floor(diff / HOUR), 'hour');
  if (diff < WEEK) return plural(Math.floor(diff / DAY), 'day');
  if (diff < MONTH) return plural(Math.floor(diff / WEEK), 'week');
  if (diff < YEAR) return plural(Math.floor(diff / MONTH), 'month');
  return plural(Math.floor(diff / YEAR), 'year');
}
