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: '更新履约配置',
  }

  return formatStatusWithRaw(action, labelMap)
}

export function formatAuditTargetType(targetType: string) {
  const labelMap: Record<string, string> = {
    admin_user: '后台用户',
    task: '交付任务',
    platform_config: '平台配置',
  }

  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 || {})
}
