/** Display helpers for push UI */

export { formatPushErrorMessage } from '@/utils/pushErrorMessage'
export { formatDateTimeBeijing as formatLogTime } from '@/utils/datetime'

export function formatSize(bytes: number): string {
  if (bytes < 1024) return `${bytes} B`
  if (bytes < 1048576) return `${(bytes / 1024).toFixed(1)} KB`
  return `${(bytes / 1048576).toFixed(1)} MB`
}

export function logStatusLabel(status: string): string {
  switch (status) {
    case 'success': return '成功'
    case 'failed': return '失败'
    case 'running': return '进行中'
    case 'cancelled': return '已取消'
    default: return status
  }
}

export function logStatusColor(status: string): string {
  switch (status) {
    case 'success': return 'success'
    case 'failed': return 'error'
    case 'cancelled': return 'grey'
    case 'running': return 'info'
    default: return 'warning'
  }
}

/** Display push_permission from sync_logs (rsync chown/chmod policy). */
export function formatPushPermission(value: string | null | undefined): string {
  if (!value) return '—'
  const parts: string[] = []
  const chown = value.match(/chown=([^\s]+)/)?.[1]
  const chmod = value.match(/chmod=([^\s]+)/)?.[1]
  if (chown) parts.push(chown)
  if (chmod) parts.push(chmod)
  return parts.length ? parts.join(' · ') : value
}

export function syncModeLabel(mode: string | undefined): string {
  switch (mode) {
    case 'incremental': return '增量'
    case 'full': return '全量'
    case 'checksum': return '校验和'
    case 'overwrite': return '覆盖'
    default: return mode || '—'
  }
}

export function pushStatusChipColor(status: string): string {
  if (status === '成功') return 'success'
  if (status === '失败') return 'error'
  if (status === '已取消') return 'grey'
  return 'warning'
}

export function serverOnlineColor(isOnline: boolean | undefined): string {
  return isOnline ? 'success' : 'error'
}
