<script setup lang="ts">
import { formatStatusWithRaw } from '@/utils/admin-display'

const props = withDefaults(
  defineProps<{
    status: string
    showRaw?: boolean
  }>(),
  {
    showRaw: true,
  },
)

type ElTagType = 'success' | 'primary' | 'warning' | 'danger' | 'info'

function resolveType(status: string): ElTagType {
  const normalized = String(status || '')
    .trim()
    .toLowerCase()

  if (
    [
      'paid',
      'link_generated',
      'redeemed',
      'available',
      'delivered',
      'success',
      'active',
      'system_bound',
      'binding_completed',
      'completed',
    ].includes(normalized)
  ) {
    return 'success'
  }

  if (
    [
      'claimed',
      'role_confirmed',
      'redeeming',
      'reserved',
      'processing',
      'admin',
      'support',
      'waiting_user_claim',
      'link_opened',
      'binding_confirmed',
      'binding_in_progress',
      'user_binding',
      'pending',
      'waiting',
      'waiting_binding',
    ].includes(normalized)
  ) {
    return 'primary'
  }

  if (
    [
      'waiting_inventory',
      'retry_pending',
      'manual_review',
      'invalid',
      'expired',
      'operator',
      'pending_binding',
      'binding_exception',
      'not_started',
      'consumed',
      'skipped',
      'pending_binding_prepare',
      'dispatched_pending_return',
    ].includes(normalized)
  ) {
    return 'warning'
  }

  if (['closed', 'failed', 'unpaid', 'revoked', 'disabled'].includes(normalized)) {
    return 'danger'
  }

  return 'info'
}

function resolveLabel(status: string) {
  const normalized = String(status || '').trim()

  if (!normalized) return '-'

  const labelMap: Record<string, string> = {
    created: '已创建',
    paid: '已支付',
    unpaid: '未支付',
    failed: '失败',
    refunded: '已退款',
    refunding: '退款中',
    closed: '已关闭',
    pending_payment: '待支付',
    waiting_inventory: '待库存',
    cdk_reserved: '已预占',
    link_generated: '已生成链接',
    claimed: '已创建会话',
    role_confirmed: '已确认角色',
    redeeming: '兑换中',
    redeemed: '已兑换',
    retry_pending: '待重试',
    manual_review: '人工处理',
    expired: '已过期',
    available: '可用',
    reserved: '已预占',
    delivered: '已发放',
    consumed: '已消耗',
    invalid: '已作废',
    active: '生效中',
    admin: '管理员',
    operator: '普通运营',
    support: '客服',
    revoked: '已撤销',
    disabled: '已停用',
    success: '成功',
    skipped: '已跳过',
    pending: '待触发',
    waiting: '等待中',
    processing: '处理中',
    system_bound: '系统已绑定',
    pending_binding: '待系统绑定',
    pending_binding_prepare: '待准备绑定',
    not_started: '未开始',
    waiting_binding: '待客户绑定',
    dispatched_pending_return: '待退还号码',
    completed: '已完成',
    waiting_user_claim: '待用户打开链接',
    link_opened: '已打开链接',
    binding_confirmed: '已提交绑定',
    binding_in_progress: '绑定处理中',
    binding_completed: '完整绑定成功',
    binding_exception: '绑定异常',
    user_binding: '用户绑定中',
  }

  if (!props.showRaw) {
    return labelMap[normalized.toLowerCase()] || normalized
  }

  return formatStatusWithRaw(normalized, labelMap)
}
</script>

<template>
  <el-tag
    :type="resolveType(props.status)"
    size="small"
    :title="props.showRaw ? undefined : props.status"
  >
    {{ resolveLabel(props.status) }}
  </el-tag>
</template>
