import { formatAdminCreditMajor } from "@/lib/money";
import type { AdminPlayerRow, AdminPlayerWalletRow } from "@/types/api/admin-player";

/** 信用额度与代理 profile 一致，按主货币整数展示（非 wallet minor）。 */
export function formatPlayerCreditAmount(amount: number, currencyCode: string): string {
  return formatAdminCreditMajor(amount, currencyCode.trim() || "NPR");
}

export function preferredDisplayWallet(row: AdminPlayerRow): AdminPlayerWalletRow | null {
  const { wallets, default_currency } = row;
  if (wallets.length === 0) {
    return null;
  }
  const code = default_currency.trim().toUpperCase();
  return wallets.find((w) => w.currency_code.toUpperCase() === code) ?? wallets[0];
}

/** 与后端 {@see PlayerFundingMode::usesCredit} 一致。 */
export function isCreditFundingPlayer(
  row: Pick<AdminPlayerRow, "funding_mode" | "uses_credit" | "available_credit" | "credit_limit">,
): boolean {
  if (row.uses_credit === true) {
    return true;
  }
  if (row.funding_mode === "credit") {
    return true;
  }
  return row.funding_mode !== "wallet" && (row.available_credit != null || row.credit_limit != null);
}

/** 主站 ↔ 彩票钱包划转单，仅钱包模式玩家适用。 */
export function playerShowsTransferOrders(
  row: Pick<AdminPlayerRow, "funding_mode" | "uses_credit" | "available_credit" | "credit_limit">,
): boolean {
  return !isCreditFundingPlayer(row);
}

export function playerBalanceCells(
  row: AdminPlayerRow,
  formatWalletMinor: (minor: number, currency: string) => string,
): { balance: string; available: string; balanceLabel: "credit" | "wallet" } {
  if (isCreditFundingPlayer(row)) {
    const ccy = row.default_currency;
    return {
      balance: formatPlayerCreditAmount(row.credit_limit ?? 0, ccy),
      available: formatPlayerCreditAmount(row.available_credit ?? 0, ccy),
      balanceLabel: "credit",
    };
  }
  const displayWallet = preferredDisplayWallet(row);
  if (!displayWallet) {
    const ccy = row.default_currency;
    return {
      balance: formatWalletMinor(0, ccy),
      available: formatWalletMinor(0, ccy),
      balanceLabel: "wallet",
    };
  }
  return {
    balance: formatWalletMinor(displayWallet.balance, displayWallet.currency_code),
    available: formatWalletMinor(displayWallet.available_balance, displayWallet.currency_code),
    balanceLabel: "wallet",
  };
}

export function playerFundingModeLabel(
  row: Pick<AdminPlayerRow, "funding_mode" | "uses_credit" | "available_credit" | "credit_limit">,
  t: (key: string, opts?: { defaultValue?: string }) => string,
): string {
  if (isCreditFundingPlayer(row)) {
    return t("players:fundingCredit", { defaultValue: "信用" });
  }
  if (row.funding_mode === "wallet") {
    return t("players:fundingWallet", { defaultValue: "钱包" });
  }
  return row.funding_mode ?? "—";
}

export function playerAuthSourceLabel(
  row: Pick<AdminPlayerRow, "auth_source">,
  t: (key: string, opts?: { defaultValue?: string }) => string,
): string {
  if (row.auth_source === "main_site_sso") {
    return t("players:authMainSite", { defaultValue: "主站 SSO" });
  }
  if (row.auth_source === "lottery_native") {
    return t("players:authNative", { defaultValue: "彩票端" });
  }
  return row.auth_source ?? "—";
}
