import { Tag } from 'antd'

type StatusTagProps = {
  value?: string | null
  kind?: 'task' | 'pay' | 'delivery' | 'resource' | 'customer' | 'role' | 'plain'
}

const STATUS_LABELS: Record<string, string> = {
  active: '生效中',
  admin: '管理员',
  available: '可用',
  binding_completed: '履约完成',
  binding_confirmed: '已提交',
  binding_exception: '履约异常',
  binding_in_progress: '处理中',
  canceled: '已取消',
  cancelled: '已取消',
  cdk_reserved: '资源已准备',
  claimed: '已创建会话',
  closed: '已关闭',
  completed: '已完成',
  consumed: '已核销',
  created: '已创建',
  customer_completed: '履约完成',
  customer_confirmed: '已提交',
  customer_exception: '履约异常',
  customer_processing: '处理中',
  delivered: '已发放',
  disabled: '已停用',
  dispatched_pending_return: '待退还号码',
  expired: '已过期',
  external: '外部链接',
  failed: '失败',
  invalid: '已作废',
  link_generated: '已生成链接',
  link_opened: '已打开链接',
  manual_review: '人工处理',
  not_started: '未开始',
  operator: '普通运营',
  paid: '已支付',
  pending: '待触发',
  pending_binding: '待准备资源',
  pending_binding_prepare: '待准备资源',
  pending_claim: '待领取',
  pending_config: '待配置',
  pending_payment: '待支付',
  pending_prepare: '待准备资源',
  processing: '处理中',
  redeemed: '已兑换',
  redeeming: '兑换中',
  refunded: '已退款',
  refunding: '退款中',
  reserved: '资源已准备',
  resource_exception: '资源异常',
  resource_ready: '资源已准备',
  revoked: '已撤销',
  retry_pending: '待重试',
  role_confirmed: '已确认角色',
  skipped: '已跳过',
  success: '成功',
  support: '客服',
  system_bound: '资源已准备',
  unpaid: '未支付',
  user_binding: '客户处理中',
  verified: '已校验',
  waiting: '等待中',
  waiting_binding: '待客户绑定',
  waiting_customer: '待客户绑定',
  waiting_user_claim: '待客户打开链接',
}

const STATUS_COLORS: Record<string, string> = {
  active: 'green',
  admin: 'purple',
  available: 'green',
  binding_completed: 'green',
  binding_confirmed: 'blue',
  binding_exception: 'red',
  binding_in_progress: 'blue',
  canceled: 'default',
  cancelled: 'default',
  cdk_reserved: 'green',
  claimed: 'blue',
  closed: 'default',
  completed: 'green',
  consumed: 'green',
  created: 'blue',
  customer_completed: 'green',
  customer_confirmed: 'blue',
  customer_exception: 'red',
  customer_processing: 'blue',
  delivered: 'green',
  disabled: 'red',
  external: 'cyan',
  failed: 'red',
  invalid: 'orange',
  link_generated: 'cyan',
  link_opened: 'blue',
  manual_review: 'orange',
  not_started: 'gold',
  operator: 'gold',
  paid: 'green',
  pending: 'gold',
  pending_binding: 'gold',
  pending_binding_prepare: 'gold',
  pending_claim: 'gold',
  pending_config: 'gold',
  pending_payment: 'default',
  pending_prepare: 'gold',
  processing: 'blue',
  redeemed: 'green',
  redeeming: 'blue',
  refunded: 'purple',
  refunding: 'purple',
  reserved: 'green',
  resource_exception: 'red',
  resource_ready: 'green',
  revoked: 'red',
  retry_pending: 'volcano',
  role_confirmed: 'blue',
  skipped: 'gold',
  success: 'green',
  support: 'blue',
  system_bound: 'green',
  unpaid: 'default',
  user_binding: 'blue',
  verified: 'green',
  waiting: 'blue',
  waiting_binding: 'gold',
  waiting_customer: 'gold',
  waiting_user_claim: 'blue',
}

export default function StatusTag({ value, kind = 'plain' }: StatusTagProps) {
  const normalized = String(value || '').trim()

  if (!normalized) {
    return <span className="muted">-</span>
  }

  const key = normalized.toLowerCase()
  const label = STATUS_LABELS[key] || normalized
  const color = STATUS_COLORS[key] || resolveFallbackColor(kind)

  return (
    <Tag color={color} title={normalized}>
      {label}
    </Tag>
  )
}

function resolveFallbackColor(kind: StatusTagProps['kind']) {
  if (kind === 'pay' || kind === 'delivery') return 'blue'
  if (kind === 'task') return 'geekblue'
  if (kind === 'resource' || kind === 'customer') return 'cyan'
  if (kind === 'role') return 'purple'
  return 'default'
}
