import { cn } from "@/lib/utils";

export function ticketStatusDisplay(
  status: string,
  winMinor: number,
  jackpotMinor: number,
  t?: (key: string, options?: { defaultValue?: string; status?: string }) => string,
  creditMode = false,
): { label: string; dotClass: string; ring?: boolean } {
  const total = winMinor + jackpotMinor;
  if (status === "partial_failed") {
    return {
      label: t?.("ticketStatus.partial_failed") ?? status,
      dotClass: "bg-amber-500",
    };
  }
  if (status === "pending_confirm" || status === "partial_pending_confirm") {
    return {
      label: t?.("ticketStatus.pending_confirm") ?? status,
      dotClass: "bg-violet-500",
    };
  }
  if (status === "refunded") {
    return {
      label: t?.("ticketStatus.refunded") ?? status,
      dotClass: "bg-slate-400",
    };
  }
  if (status === "pending_draw" || status === "placed") {
    return {
      label: t?.("ticketStatus.pending_draw") ?? status,
      dotClass: "bg-sky-500",
    };
  }
  if (status === "pending_payout") {
    return {
      label: creditMode
        ? t?.("ticketStatus.credit.pending_payout", { defaultValue: "已中奖待结算" }) ?? status
        : t?.("ticketStatus.pending_payout") ?? status,
      dotClass: "bg-amber-500",
    };
  }
  if (status === "settled_win" && total > 0) {
    return {
      label: creditMode
        ? t?.("ticketStatus.credit.settled_win", { defaultValue: "已中奖" }) ?? status
        : t?.("ticketStatus.settled_win") ?? status,
      dotClass: "bg-emerald-500",
    };
  }
  if (status === "settled_lose" || (status === "settled_win" && total <= 0)) {
    return {
      label: t?.("ticketStatus.settled_lose") ?? status,
      dotClass: "bg-background",
      ring: true,
    };
  }
  if (status === "failed") {
    return { label: t?.("ticketStatus.failed") ?? status, dotClass: "bg-red-500" };
  }
  return {
    label: t?.("ticketStatus.unknown", { status, defaultValue: status }) ?? status,
    dotClass: "bg-red-500",
  };
}

export function StatusDot({
  label,
  dotClass,
  ring,
}: {
  label: string;
  dotClass: string;
  ring?: boolean;
}) {
  return (
    <span className="inline-flex shrink-0 items-center gap-1.5 rounded-full border border-[#e5edf8] bg-[#f8fbff] px-2 py-1 text-xs font-bold text-slate-600">
      <span
        className={cn(
          "inline-block size-2 shrink-0 rounded-full",
          dotClass,
          ring && "ring-1 ring-muted-foreground/50",
        )}
        aria-hidden
      />
      <span>{label}</span>
    </span>
  );
}
