type LabelMap = Record<string, string>

function resolveLabel(
  labels: LabelMap,
  value?: string | null,
  emptyLabel = '-'
) {
  if (!value) return emptyLabel
  return labels[value] ?? value
}

const statusLabels: LabelMap = {
  active: '正常',
  suspended: '已暂停',
  banned: '已封禁',
  expired: '已过期',
  revoked: '已吊销',
  received: '正常接收',
  quarantined: '已隔离',
  rejected: '已拒绝',
  sent: '发送成功',
  failed: '发送失败',
  pending: '待处理',
  healthy: '正常',
  fault: '故障',
  degraded: '降级',
  disabled: '未启用',
  starting: '启动中',
  scheduled: '已调度',
  enabled: '已启用',
  unknown: '未知',
}

const roleLabels: LabelMap = {
  admin: '管理员',
  user: '普通用户',
}

const emailKindLabels: LabelMap = {
  verification: '验证码',
  verification_code: '验证码',
  demo_verification: '验证码（演示）',
  password_reset: '密码重置',
  demo_password_reset: '密码重置（演示）',
  notification: '系统通知',
  security: '安全通知',
  registration: '注册通知',
}

const securityResultLabels: LabelMap = {
  pass: '通过',
  fail: '失败',
  softfail: '软失败',
  neutral: '中立',
  none: '无结果',
  unknown: '未知',
  missing: '缺失',
  header_present: '检测到签名头',
  reject: '拒绝',
  low: '低风险',
  medium: '中风险',
  high: '高风险',
}

const auditEventLabels: LabelMap = {
  admin: '管理操作',
  admin_email_action: '邮件治理',
  admin_email_view: '邮件查看',
  admin_attachment_download: '附件下载',
  automatic_block: '自动封禁',
  blacklist: '黑名单操作',
  email_governance: '邮件治理',
  smtp: 'SMTP 会话',
  smtp_accept: 'SMTP 接收',
  smtp_honeypot: '蜜罐命中',
  user_status_change: '用户状态变更',
  mailbox_revoke: '邮箱吊销',
  credit_adjust: '积分调整',
  credit_rule_change: '积分规则修改',
}

const auditActionLabels: LabelMap = {
  accept: '接收',
  accepted: '已接收',
  reject: '拒绝',
  rejected: '已拒绝',
  quarantine: '隔离',
  quarantined: '已隔离',
  release: '放行',
  released: '已放行',
  view: '查看',
  viewed: '已查看',
  download: '下载',
  delete: '删除',
  revoke: '吊销',
  activate: '恢复',
  suspend: '暂停',
  ban: '封禁',
  block_sender: '封禁发件人',
  block_domain: '封禁发件域',
  block_ip: '封禁来源 IP',
  remove: '解除',
  adjust: '调整',
  update: '修改',
  reset: '恢复默认值',
}

const targetTypeLabels: LabelMap = {
  user: '用户',
  mailbox: '邮箱',
  email: '邮件',
  attachment: '附件',
  sender: '发件人',
  sender_block: '发件人封禁',
  ip: '来源 IP',
  ip_block: 'IP 封禁',
  credit: '积分',
  credit_rule: '积分规则',
}

const creditReasonLabels: LabelMap = {
  register_bonus: '注册赠送',
  migration_bonus: '存量迁移赠送',
  daily_check_in: '每日签到',
  create_mailbox: '创建邮箱',
  receive_email: '接收邮件',
  admin_adjust: '管理员调整',
}

const blacklistKindLabels: LabelMap = {
  address: '完整地址',
  email: '邮件地址',
  domain: '发件域',
  pattern: '匹配模式',
  ip: '来源 IP',
}

const sourceLabels: LabelMap = {
  auto: '自动',
  automatic: '自动',
  manual: '人工',
  system: '系统',
}

const displayTextLabels: LabelMap = {
  'Scale governance fixture': '规模化治理测试数据',
  'Policy rejected synthetic SMTP event': '策略拒绝的模拟 SMTP 事件',
  'Automatic threshold reached': '达到自动封禁阈值',
  'Demo SMTP upstream timeout': '演示 SMTP 上游超时',
  'Scale provider timeout': '测试邮件服务商超时',
  'Demo transaction': '演示积分流水',
  'Demo manual correction': '演示人工调整',
}

export const formatStatusLabel = (value?: string | null) =>
  resolveLabel(statusLabels, value)

export const formatRoleLabel = (value?: string | null) =>
  resolveLabel(roleLabels, value)

export const formatEmailKindLabel = (value?: string | null) =>
  resolveLabel(emailKindLabels, value)

export const formatSecurityResultLabel = (value?: string | null) =>
  resolveLabel(securityResultLabels, value, '未知')

export const formatAuditEventLabel = (value?: string | null) =>
  resolveLabel(auditEventLabels, value)

export const formatAuditActionLabel = (value?: string | null) =>
  resolveLabel(auditActionLabels, value)

export const formatTargetTypeLabel = (value?: string | null) =>
  resolveLabel(targetTypeLabels, value)

export const formatCreditReasonLabel = (value?: string | null) =>
  resolveLabel(creditReasonLabels, value)

export const formatBlacklistKindLabel = (value?: string | null) =>
  resolveLabel(blacklistKindLabels, value)

export const formatSourceLabel = (value?: string | null) =>
  resolveLabel(sourceLabels, value)

export function formatAdminText(value?: string | null) {
  if (!value) return '-'
  const scaleTransaction = /^Scale transaction (\d+)$/.exec(value)
  if (scaleTransaction) {
    return `规模测试流水 #${scaleTransaction[1]}`
  }
  return displayTextLabels[value] ?? value
}
