/**
 * Shared formatting utilities — import from here, never redefine inline.
 */

export function formatDate(iso) {
  if (!iso) return '—'
  return new Date(iso).toLocaleDateString(undefined, {
    month: 'short',
    day: 'numeric',
    year: 'numeric',
  })
}

export function formatSize(bytes) {
  if (!bytes) return '—'
  if (bytes < 1024) return bytes + ' B'
  if (bytes < 1048576) return (bytes / 1024).toFixed(1) + ' KB'
  return (bytes / 1048576).toFixed(1) + ' MB'
}

/** Formats an ISO timestamp for audit log display: YYYY-MM-DD HH:MM:SS */
export function formatTimestamp(iso) {
  if (!iso) return '—'
  try {
    return new Date(iso).toISOString().replace('T', ' ').slice(0, 19)
  } catch {
    return iso
  }
}

/** Maps a cloud provider slug to a Tailwind text-color class. */
export function providerColor(provider) {
  const map = {
    google_drive: 'text-blue-500',
    onedrive: 'text-sky-500',
    nextcloud: 'text-orange-500',
    webdav: 'text-gray-500',
  }
  return map[provider] ?? 'text-gray-400'
}

/** Maps a cloud provider slug to a Tailwind background-color class. */
export function providerBg(provider) {
  const map = {
    google_drive: 'bg-blue-50',
    onedrive: 'bg-sky-50',
    nextcloud: 'bg-orange-50',
    webdav: 'bg-gray-50',
  }
  return map[provider] ?? 'bg-gray-50'
}

/**
 * Returns a human-readable relative time string for an ISO timestamp.
 * e.g. "just now", "2 minutes ago", "1 hour ago", "3 days ago"
 */
export function formatRelativeTime(iso) {
  if (!iso) return ''
  const diff = Math.floor((Date.now() - new Date(iso).getTime()) / 1000)
  if (diff < 10) return 'just now'
  if (diff < 60) return `${diff} seconds ago`
  const mins = Math.floor(diff / 60)
  if (mins < 60) return mins === 1 ? '1 minute ago' : `${mins} minutes ago`
  const hrs = Math.floor(mins / 60)
  if (hrs < 24) return hrs === 1 ? '1 hour ago' : `${hrs} hours ago`
  const days = Math.floor(hrs / 24)
  return days === 1 ? '1 day ago' : `${days} days ago`
}

/** Human-readable label for a cloud provider slug. */
export function providerLabel(provider) {
  const map = {
    google_drive: 'Google Drive',
    onedrive: 'OneDrive',
    nextcloud: 'Nextcloud',
    webdav: 'WebDAV',
  }
  return map[provider] ?? provider
}
