/**
 * 后台状态色标：统一映射到可读色调，列表/详情一眼可辨。
 */
export type AdminStatusTone =
  | "success"
  | "info"
  | "warning"
  | "danger"
  | "neutral"
  | "indigo"
  | "draft"
  | "active"
  | "archived";

export const ADMIN_STATUS_TONE_CLASSES: Record<AdminStatusTone, string> = {
  success:
    "border-emerald-500/30 bg-emerald-500/12 text-emerald-800 dark:text-emerald-300",
  info: "border-primary/30 bg-primary/10 text-primary",
  warning:
    "border-amber-500/30 bg-amber-500/12 text-amber-900 dark:text-amber-300",
  danger:
    "border-destructive/35 bg-destructive/10 text-destructive dark:text-red-300",
  neutral: "border-border/80 bg-muted text-muted-foreground",
  indigo:
    "border-indigo-500/30 bg-indigo-500/12 text-indigo-800 dark:text-indigo-300",
  draft:
    "border-amber-500/30 bg-amber-500/12 text-amber-900 dark:text-amber-300",
  active:
    "border-emerald-500/30 bg-emerald-500/12 text-emerald-800 dark:text-emerald-300",
  archived:
    "border-slate-400/35 bg-slate-500/10 text-slate-600 dark:text-slate-300",
};

const STATUS_TONE_MAP: Record<string, AdminStatusTone> = {
  // 配置版本
  draft: "draft",
  active: "active",
  archived: "archived",

  // 期次
  open: "info",
  closing: "warning",
  closed: "neutral",
  drawing: "indigo",
  review: "warning",
  cooldown: "neutral",
  pending: "info",
  settling: "indigo",
  settled: "success",
  cancelled: "danger",

  // 结算批次
  running: "warning",
  pending_review: "info",
  approved: "indigo",
  rejected: "neutral",
  paid: "success",
  completed: "success",
  failed: "danger",

  // 注单
  pending_confirm: "info",
  partial_pending_confirm: "warning",
  success: "success",
  pending_payout: "warning",
  settled_win: "success",
  settled_lose: "neutral",

  // 钱包 / 划转
  processing: "info",
  posted: "success",
  pending_reconcile: "warning",
  reversed: "neutral",
  manually_processed: "indigo",

  // 对账
  mismatch: "danger",
  matched: "success",
  pending_check: "warning",

  // 开关 / 布尔
  enabled: "success",
  disabled: "neutral",
  true: "success",
  false: "neutral",

  // 玩家 status 数值
  "0": "success",
  "1": "warning",
  "2": "danger",

  // 管理员/角色（1=启用 0=停用）
  "role_enabled": "success",
  "role_disabled": "neutral",
};

/** 玩家：0 正常 1 冻结 2 封禁 */
export function resolvePlayerStatusTone(status: number): AdminStatusTone {
  if (status === 0) return "success";
  if (status === 1) return "warning";
  if (status === 2) return "danger";
  return "neutral";
}

/** 后台用户：0 启用 1 停用 */
export function resolveAdminUserStatusTone(status: number): AdminStatusTone {
  return status === 0 ? "success" : "neutral";
}

/** 角色：1 启用 0 停用 */
export function resolveRoleStatusTone(status: number): AdminStatusTone {
  return status === 1 ? "success" : "neutral";
}

export function resolveAdminStatusTone(
  status: string | number | boolean | null | undefined,
): AdminStatusTone {
  if (status === null || status === undefined || status === "") {
    return "neutral";
  }
  if (typeof status === "boolean") {
    return status ? "success" : "neutral";
  }
  const key = String(status).trim().toLowerCase();
  return STATUS_TONE_MAP[key] ?? "neutral";
}

export function adminStatusBadgeClassName(
  tone: AdminStatusTone,
  className?: string,
): string {
  return `${ADMIN_STATUS_TONE_CLASSES[tone]} ${className ?? ""}`.trim();
}
