"use client";

import { useEffect } from "react";
import { useTranslation } from "react-i18next";
import { toast } from "sonner";

import { getLotteryEcho } from "@/lib/lottery-echo";
import { useActivePlayerCurrency } from "@/hooks/use-active-player-currency";
import { usePlayerSessionStore } from "@/stores/player-session-store";

type BalanceUpdateWsEvent = {
  player_id?: number;
  currency_code?: string;
  balance_minor?: number;
  change_minor?: number;
  change_formatted?: string;
  reason?: string;
};

const REASON_I18N_KEY: Record<string, string> = {
  transfer_in: "wallet.wsReason.transferIn",
  transfer_out: "wallet.wsReason.transferOut",
  bet: "wallet.wsReason.bet",
  prize: "wallet.wsReason.prize",
  refund: "wallet.wsReason.refund",
};

/**
 * 订阅 `player.{id}` 私有频道的 `balance.update`，刷新余额并 Toast。
 */
export function usePlayerBalanceWs(): void {
  const { t } = useTranslation("player");
  const playerId = usePlayerSessionStore((state) => state.profile?.id);
  const bearerToken = usePlayerSessionStore((state) => state.bearerToken);
  const { activeCurrency } = useActivePlayerCurrency();

  useEffect(() => {
    if (!playerId || !bearerToken) {
      return;
    }

    const echo = getLotteryEcho();
    if (!echo) {
      return;
    }

    const channelName = `player.${playerId}`;
    const channel = echo.channel(channelName);

    const onBalanceUpdate = (evt: BalanceUpdateWsEvent): void => {
      const currency = evt.currency_code?.trim().toUpperCase();
      if (currency && currency !== activeCurrency.toUpperCase()) {
        return;
      }

      window.dispatchEvent(new Event("lottery-wallet-refresh"));

      const changeLabel =
        typeof evt.change_formatted === "string" && evt.change_formatted !== ""
          ? evt.change_formatted
          : evt.change_minor != null
            ? String(evt.change_minor)
            : "";

      const reasonKey =
        evt.reason && REASON_I18N_KEY[evt.reason]
          ? REASON_I18N_KEY[evt.reason]
          : "wallet.wsReason.unknown";
      const reasonLabel = t(reasonKey);

      toast.message(t("wallet.wsBalanceUpdated", { change: changeLabel, reason: reasonLabel }));
    };

    channel.listen(".balance.update", onBalanceUpdate);

    return () => {
      channel.stopListening(".balance.update");
    };
  }, [activeCurrency, bearerToken, playerId, t]);
}
