import { ShieldAlert, ShieldCheck, ShieldX } from "lucide-react";
import { cn } from "@/lib/utils";
import { adminPaymentsTexts as t } from "@/lib/i18n/admin-payments";

/**
 * Fraud risk chip. Signalling only — it never blocks a payment.
 * Reasons come from `Payment.payload.reasons[]` and show as a native tooltip.
 */

const TONE: Record<string, string> = {
  low: "bg-emerald-100 text-emerald-700 dark:bg-emerald-500/15 dark:text-emerald-400",
  medium: "bg-amber-100 text-amber-700 dark:bg-amber-500/15 dark:text-amber-400",
  high: "bg-rose-100 text-rose-700 dark:bg-rose-500/15 dark:text-rose-400",
};

const LABEL: Record<string, string> = {
  low: t.riskLow,
  medium: t.riskMedium,
  high: t.riskHigh,
};

export function FraudChip({
  level,
  reasons = [],
}: {
  level?: string | null;
  reasons?: string[];
}) {
  const key = (level ?? "low").toLowerCase();
  const tone = TONE[key] ?? TONE.low;
  const label = LABEL[key] ?? t.riskLow;
  const title = reasons.length
    ? `${t.riskReasons}:\n• ${reasons.join("\n• ")}`
    : t.riskNoSignals;

  const Icon = key === "high" ? ShieldX : key === "medium" ? ShieldAlert : ShieldCheck;

  return (
    <span
      title={title}
      className={cn(
        "inline-flex items-center gap-1 rounded-full px-2 py-0.5 text-xs font-medium whitespace-nowrap",
        tone,
      )}
    >
      <Icon className="size-3 shrink-0" />
      {label}
      {reasons.length ? (
        <span className="font-mono text-[10px] tabular-nums opacity-70">
          {reasons.length}
        </span>
      ) : null}
    </span>
  );
}

export default FraudChip;
