import type { BackendConsultingRequest, BackendDataServiceRequest, BackendStaffAccount, BackendTask } from '@/api/backend'

export function hasPermission(account: BackendStaffAccount | null, permission: string) {
  const permissions = account?.permissions || []
  return permissions.includes('*') || permissions.includes(permission)
}

export function formatDateTime(value?: string | null) {
  if (!value) return '-'
  const date = new Date(value)
  if (Number.isNaN(date.getTime())) return '-'
  return `${date.getFullYear()}-${String(date.getMonth() + 1).padStart(2, '0')}-${String(date.getDate()).padStart(2, '0')} ${String(date.getHours()).padStart(2, '0')}:${String(date.getMinutes()).padStart(2, '0')}`
}

export function taskStatusLabel(status?: string) {
  if (status === 'pending_material') return '待提交资料'
  if (status === 'assigned') return '待接单'
  if (status === 'in_progress') return '配置中'
  if (status === 'pending_confirm') return '待用户确认'
  if (status === 'completed') return '已完成'
  if (status === 'cancelled') return '已取消'
  return '待处理'
}

export function consultingStatusLabel(status?: BackendConsultingRequest['status']) {
  if (status === 'pending') return '待接单'
  if (status === 'assigned') return '已接单'
  if (status === 'communicating') return '沟通中'
  if (status === 'planning') return '方案制定中'
  if (status === 'proposal_ready') return '待确认报价'
  if (status === 'pending_payment') return '待支付'
  if (status === 'in_service') return '服务中'
  if (status === 'completed') return '已完成'
  if (status === 'cancelled') return '已取消'
  return '待处理'
}

export function dataServiceStatusLabel(status?: BackendDataServiceRequest['status']) {
  if (status === 'pending_contact') return '等待联系'
  if (status === 'contacting') return '沟通中'
  if (status === 'quoted') return '待付款'
  if (status === 'pending_payment') return '待支付'
  if (status === 'in_service') return '服务中'
  if (status === 'completed') return '已完成'
  if (status === 'cancelled') return '已取消'
  return '待处理'
}

export function taskSummary(item: BackendTask) {
  return `${item.orderNo || item.orderId} · ${taskStatusLabel(item.status)}`
}
