type TagType = 'primary' | 'success' | 'warning' | 'danger' | 'info'

const STATUS_LABELS: Record<string, string> = {
  pending: '等待中',
  syncing: '同步中',
  queued: '排队中',
  running: '运行中',
  completed: '已完成',
  failed: '失败',
  stopped: '已停止',
  cancelled: '已取消',
  starting: '启动中',
  loading: '加载中',
  loaded: '已加载',
  ready: '已就绪',
  done: '已完成',
  error: '异常',
  success: '成功',
  not_started: '未启动',
  valid: '有效',
  modified: '已修改',
  invalid: '无效',
  original: '原始',
  manual: '手动新增',
  active: '启用',
  disabled: '停用',
  online: '在线',
  offline: '离线',
  draining: '维护中',
  maintenance: '维护模式',
  busy: '忙碌',
  reserved: '已预留',
  idle: '空闲',
  warning: '告警',
  available: '可用',
  missing: '缺失',
  synced: '已同步',
  drifted: '已漂移',
  repair_pending: '待修复',
}

const STATUS_TYPES: Record<string, TagType> = {
  completed: 'success',
  success: 'success',
  running: 'success',
  ready: 'success',
  loaded: 'success',
  active: 'success',
  online: 'success',
  busy: 'success',
  available: 'success',
  synced: 'success',
  valid: 'success',
  pending: 'info',
  idle: 'info',
  offline: 'info',
  disabled: 'info',
  original: 'info',
  stopped: 'info',
  cancelled: 'info',
  syncing: 'warning',
  queued: 'warning',
  starting: 'warning',
  loading: 'warning',
  reserved: 'warning',
  warning: 'warning',
  modified: 'warning',
  draining: 'warning',
  maintenance: 'warning',
  repair_pending: 'warning',
  failed: 'danger',
  error: 'danger',
  invalid: 'danger',
  missing: 'danger',
  drifted: 'danger',
}

export function statusLabel(status?: string | number | null) {
  const key = String(status ?? '').trim()
  if (!key) return '未知'
  return STATUS_LABELS[key] || key
}

export function statusTagType(status?: string | number | null): TagType {
  const key = String(status ?? '').trim()
  return STATUS_TYPES[key] || 'info'
}

export function mergeStatusLabel(row: { merged?: boolean; merging?: boolean }) {
  if (row.merging) return '合并中'
  if (row.merged) return '已合并'
  return '未合并'
}

export function mergeStatusType(row: { merged?: boolean; merging?: boolean }): TagType {
  if (row.merging) return 'warning'
  if (row.merged) return 'success'
  return 'info'
}
