export function formatStatusWithRaw(status: string, labelMap: Record<string, string> = {}) {
  const normalized = String(status || '').trim()

  if (!normalized) {
    return '-'
  }

  const key = normalized.toLowerCase()
  const label = labelMap[key]

  if (!label || label === normalized) {
    return normalized
  }

  return `${label} (${normalized})`
}

export function formatAuditAction(action: string) {
  const labelMap: Record<string, string> = {
    admin_user_created: '创建后台用户',
    admin_user_role_updated: '修改用户角色',
    admin_user_status_updated: '修改用户状态',
    admin_user_password_reset: '重置用户密码',
    task_regenerate_claim_link: '重发领取链接',
    task_kuaishou_cloud_rebind_role: '换绑角色',
    task_closed: '关闭任务',
    task_mark_manual_review: '转人工处理',
    task_complete_manual_dispatch: '完成人工履约',
    platform_shop_config_updated: '更新店铺配置',
    platform_fulfillment_config_updated: '更新履约配置',
    worker_finance_request_reviewed: '资金申请审核',
    worker_cancel_request_reviewed: '打手退单申请审核',
    worker_reviewed: '打手账号审核',
    worker_wallet_credited: '打手钱包手动加款',
    worker_password_reset: '重置打手密码',
    worker_withdrawal_account_saved: '打手提现账户保存',
    work_order_published: '工单发布',
    work_order_unpublished: '工单下架',
    work_order_updated: '工单信息更新',
    work_order_deleted: '工单删除',
    work_order_accepted: '工单验收',
    work_orders_batch_accepted: '批量验收',
    work_order_marked_problem: '工单标记问题',
    work_order_problem_resolved: '问题单处置',
    work_order_reopened: '工单重新打开',
    work_order_pin_toggled: '工单置顶切换',
    work_order_worker_assigned: '工单指派打手',
    work_order_worker_unassigned: '工单取消指派',
    work_order_material_saved: '工单资料保存',
    work_order_share_accepted: '拼单份额验收',
    work_order_share_cancelled: '拼单份额取消',
    work_order_sharing_config_updated: '拼单配置更新',
    work_order_pending_deposit_deducted: '押金扣除',
    after_sales_case_created: '售后问题单创建',
    after_sales_case_resolved: '售后问题单处置',
    worker_level_saved: '接单等级保存',
    worker_level_deleted: '接单等级删除',
    work_category_saved: '接单分类保存',
    work_category_deleted: '接单分类删除',
    work_product_rule_saved: '接单模板保存',
    work_product_rule_deleted: '接单模板删除',
    work_product_rule_price_synced: '模板价格同步',
    work_product_mapping_saved: '商品映射保存',
    worker_finance_config_saved: '资金配置保存',
    worker_hall_config_saved: '大厅配置保存',
    worker_announcement_saved: '公告保存',
    worker_platform_notification_config_saved: '通知配置保存',
    worker_product_match_config_saved: '商品匹配配置保存',
  }

  return formatStatusWithRaw(action, labelMap)
}

export function formatAuditTargetType(targetType: string) {
  const labelMap: Record<string, string> = {
    admin_user: '后台用户',
    task: '交付任务',
    platform_config: '平台配置',
    worker: '打手',
    work_order: '接单工单',
    work_order_share: '拼单份额',
    worker_finance_request: '资金申请',
    worker_cancel_request: '打手退单申请',
    after_sales_case: '售后问题单',
    worker_level: '接单等级',
    work_category: '接单分类',
    work_product_rule: '接单模板',
    work_product_mapping: '商品映射',
  }

  return formatStatusWithRaw(targetType, labelMap)
}

export function formatTaskEventType(eventType: string) {
  const labelMap: Record<string, string> = {
    manual_dispatch_completed: '人工履约已回写',
    claim_redeem_code_used: '兑换码已使用',
    claim_redeem_code_invalid: '兑换码错误',
    claim_redeem_completed: '兑换链路完成',
    kuaishou_cloud_rebind_requested: '发起角色换绑',
    kuaishou_cloud_rebind_old_number_returned: '换绑旧号已退还',
    kuaishou_cloud_rebind_prepared: '换绑资源已准备',
    kuaishou_cloud_rebind_failed: '换绑失败',
  }

  return formatStatusWithRaw(eventType, labelMap)
}

export function formatRedeemOutcomeLabel(value: string) {
  const normalized = String(value || '').trim()

  switch (normalized) {
    case 'success':
      return '兑换成功'
    case 'code_used':
      return '兑换码已使用'
    case 'code_invalid':
      return '兑换码错误'
    case 'captcha_rejected':
      return '验证码错误'
    case 'failed':
      return '兑换失败'
    default:
      return normalized || '-'
  }
}

export function formatRedeemResolutionStatus(value: string) {
  return value === 'success' ? '已完成' : value === 'failed' ? '失败收口' : value || '-'
}

export function formatKuaishouRoleInfoLabel(value: string) {
  const normalized = String(value || '').trim()

  switch (normalized) {
    case 'name':
      return '角色名称'
    case 'rid':
      return '角色 ID'
    default:
      return normalized || '-'
  }
}

export function formatTaskEventPayload(payload: Record<string, unknown>) {
  const parts = [
    payload.outcome ? `结果 ${payload.outcome}` : '',
    payload.resultCode ? `代码 ${payload.resultCode}` : '',
    payload.resultMessage ? `说明 ${payload.resultMessage}` : '',
    payload.codeMasked ? `凭据 ${payload.codeMasked}` : '',
    payload.previousCodeMasked ? `旧码 ${payload.previousCodeMasked}` : '',
    payload.nextCodeMasked ? `新码 ${payload.nextCodeMasked}` : '',
    payload.ticketCodeMasked ? `卡券号 ${payload.ticketCodeMasked}` : '',
    payload.vnId ? `虚拟号 #${payload.vnId}` : '',
    payload.oldVnId ? `旧虚拟号 #${payload.oldVnId}` : '',
    payload.vnPhoneMasked ? `号码 ${payload.vnPhoneMasked}` : '',
    payload.oldVnPhoneMasked ? `旧号码 ${payload.oldVnPhoneMasked}` : '',
    payload.oldRoleName ? `旧角色 ${payload.oldRoleName}` : '',
    payload.oldRoleId ? `旧角色ID ${payload.oldRoleId}` : '',
    payload.attempt ? `第 ${payload.attempt} 次` : '',
    payload.bindUrl ? `绑定链接 ${payload.bindUrl}` : '',
    payload.sendType ? `发送类型 ${payload.sendType}` : '',
    payload.note ? `备注 ${payload.note}` : '',
    payload.previousOutcome
      ? `切换原因 ${formatRedeemOutcomeLabel(String(payload.previousOutcome || ''))}`
      : '',
    payload.deliveryReference ? `单号 ${payload.deliveryReference}` : '',
    payload.platformOrderId ? `平台单 ${payload.platformOrderId}` : '',
    payload.trigger ? `触发 ${payload.trigger}` : '',
    payload.reason ? `原因 ${payload.reason}` : '',
    payload.purchaseTriggered === true ? '已触发购买' : '',
    payload.usedKnapsack === true ? '使用背包库存' : '',
    payload.responseStatus ? `HTTP ${payload.responseStatus}` : '',
    payload.requestId ? `请求 ${payload.requestId}` : '',
    payload.errorMessage ? `错误 ${payload.errorMessage}` : '',
  ].filter(Boolean)

  if (parts.length > 0) {
    return parts.join(' · ')
  }

  return JSON.stringify(payload || {})
}
