"use client";

import { useTranslation } from "react-i18next";

import { AdminStatusBadge } from "@/components/admin/admin-status-badge";
import { isCreditFundingPlayer, playerFundingModeLabel } from "@/lib/admin-player-display";
import { resolveLedgerSourceTone } from "@/lib/admin-status-tone";
import { cn } from "@/lib/utils";

type FundingRow = {
  funding_mode?: string | null;
  uses_credit?: boolean;
  available_credit?: number | null;
  credit_limit?: number | null;
};

export function PlayerFundingModeBadge({
  row,
  className,
}: {
  row: FundingRow;
  className?: string;
}): React.ReactElement {
  const { t } = useTranslation("players");
  const normalized = {
    funding_mode: row.funding_mode ?? undefined,
    uses_credit: row.uses_credit,
    available_credit: row.available_credit ?? undefined,
    credit_limit: row.credit_limit ?? undefined,
  };
  const isCredit = isCreditFundingPlayer(normalized);

  return (
    <span
      className={cn(
        "inline-flex rounded-md border px-2 py-0.5 text-xs font-medium",
        isCredit
          ? "border-violet-200 bg-violet-50 text-violet-900 dark:border-violet-800 dark:bg-violet-950/50 dark:text-violet-200"
          : "border-sky-200 bg-sky-50 text-sky-900 dark:border-sky-800 dark:bg-sky-950/50 dark:text-sky-200",
        className,
      )}
    >
      {playerFundingModeLabel(normalized, t)}
    </span>
  );
}

export function PlayerLedgerSourceBadge({
  ledgerSource,
  className,
}: {
  ledgerSource?: string | null;
  className?: string;
}): React.ReactElement | null {
  const { t } = useTranslation("wallet");
  if (
    ledgerSource !== "credit_ledger" &&
    ledgerSource !== "wallet_txn" &&
    ledgerSource !== "payment_record" &&
    ledgerSource !== "settlement_adjustment" &&
    ledgerSource !== "share_ledger"
  ) {
    return null;
  }

  const label =
    ledgerSource === "wallet_txn"
      ? t("ledgerWallet", { defaultValue: "钱包流水" })
      : ledgerSource === "payment_record"
        ? t("ledgerPayment", { defaultValue: "收付记录" })
        : ledgerSource === "settlement_adjustment"
          ? t("ledgerAdjustment", { defaultValue: "调账记录" })
          : ledgerSource === "share_ledger"
            ? t("ledgerShare", { defaultValue: "分账" })
            : t("ledgerCredit", { defaultValue: "信用流水" });

  return (
    <AdminStatusBadge status={ledgerSource} tone={resolveLedgerSourceTone(ledgerSource)} className={className}>
      {label}
    </AdminStatusBadge>
  );
}
