/**
 * 后台状态色标：统一映射到可读色调，列表/详情一眼可辨。
 */
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",

  // 代理账期 / 账单
  confirmed: "warning",
  partial_paid: "warning",
  overdue: "danger",
  reversed: "neutral",

  // 报表导出任务
  queued: "info",

  // 开奖结果批次
  published: "success",

  // 对账明细
  resolved: "success",
  unresolved: "warning",
  stale_processing: "warning",
  missing_wallet_txn: "danger",
  unexpected_wallet_txn: "danger",
  missing_refund: "warning",
  missing_reversal: "warning",
  main_site_record_missing: "warning",
  main_site_failed: "danger",
  main_site_unavailable: "neutral",

  // 注单
  pending_confirm: "info",
  partial_pending_confirm: "warning",
  success: "success",
  pending_payout: "warning",
  settled_win: "success",
  settled_lose: "neutral",
  placed: "neutral",
  pending_draw: "info",
  partial_failed: "warning",
  refunded: "neutral",

  // 钱包 / 划转
  processing: "info",
  posted: "success",
  pending_reconcile: "warning",
  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 resolveRiskPoolStatusTone(
  row: { is_sold_out?: boolean; usage_ratio?: number | null },
  highRiskThreshold = 0.8,
): AdminStatusTone {
  if (row.is_sold_out) {
    return "danger";
  }
  if ((row.usage_ratio ?? 0) >= highRiskThreshold) {
    return "warning";
  }
  return "success";
}

/** 账务流水行：优先账单状态，否则入账状态 */
export function resolveCreditLedgerRowStatusTone(
  row: { bill_status?: string | null; status?: string | null },
): AdminStatusTone {
  if (row.bill_status) {
    return resolveAdminStatusTone(row.bill_status);
  }
  return resolveAdminStatusTone(row.status ?? "posted");
}

/** 流水来源渠道色标 */
export function resolveLedgerSourceTone(
  ledgerSource: string | null | undefined,
): AdminStatusTone {
  switch (ledgerSource) {
    case "wallet_txn":
    case "payment_record":
      return "success";
    case "credit_ledger":
      return "info";
    case "settlement_adjustment":
      return "warning";
    case "share_ledger":
      return "indigo";
    default:
      return "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();
}
