"use client";

export function FilmCameraIcon({
  size = 18,
  color = "currentColor",
  strokeWidth = 2.25,
  style,
  ...props
}: React.SVGProps<SVGSVGElement> & { size?: number; color?: string; strokeWidth?: number }) {
  return (
    <svg
      width={size}
      height={size}
      viewBox="0 0 24 24"
      fill="none"
      stroke={color}
      strokeWidth={strokeWidth}
      strokeLinecap="round"
      strokeLinejoin="round"
      style={{ flex: "none", ...style }}
      {...props}
    >
      {/* Top reels */}
      <circle cx="7" cy="5.5" r="2.2" />
      <circle cx="13.5" cy="5" r="3.2" />
      {/* Camera body */}
      <rect x="3" y="9" width="12" height="11" rx="3" />
      {/* Projector / lens cone */}
      <path d="M15 12.5l4.8-2.6c.7-.4 1.7.1 1.7 1v6.2c0 .9-1 1.4-1.7 1L15 15.5" />
    </svg>
  );
}


import {
  BadgeCheck,
  Bug,
  Activity,
  Award,
  Bot,
  Crown,
  Code2,
  Flame,
  Gem,
  HandHeart,
  HandCoins,
  Handshake,
  Heart,
  PartyPopper,
  Rocket,
  Scale,
  Shield,
  ShieldCheck,
  Sparkles,
  Sparkle,
  Trophy,
  WandSparkles,
  Coins,
  Star,
  Moon,
  Megaphone,
} from "lucide-react";
import type { BadgeItem } from "@/lib/constants";

// eslint-disable-next-line @typescript-eslint/no-explicit-any
const ICONS: Record<string, React.ComponentType<any>> = {
  owner: Crown,
  staff: Shield,
  helper: WandSparkles,
  verified: BadgeCheck,
  premium: Gem,
  donor: HandHeart,
  donator: HandHeart,
  og: Award,
  rich: Rocket,
  bug: Bug,
  "bug-finder": Bug,
  booster: Rocket,
  winner: Trophy,
  early: HandCoins,
  founding100: PartyPopper,
  founding1k: Sparkle,
  "discord-staff": ShieldCheck,
  "discord-partner": Handshake,
  "discord-hypesquad": Sparkles,
  "discord-bug-hunter": Bug,
  "discord-hypesquad-bravery": Flame,
  "discord-hypesquad-brilliance": Gem,
  "discord-hypesquad-balance": Scale,
  "discord-early-supporter": Heart,
  "discord-bug-hunter-level-2": Bug,
  "discord-verified-bot": Bot,
  "discord-verified-developer": Code2,
  "discord-certified-moderator": Shield,
  "discord-active-developer": Activity,
  "discord-nitro": Gem,
  "discord-server-booster": Rocket,
  "coin-collector": Coins,
  stardust: Star,
  "night-owl": Moon,
  promoter: Megaphone,
  creator: FilmCameraIcon,
};

const COLORS: Record<string, string> = {
  owner: "#ffffff",
  staff: "#ffffff",
  helper: "#e6b0b0",
  verified: "#e6a8a8",
  premium: "#ffffff",
  donor: "#ffffff",
  donator: "#ffffff",
  og: "#ffffff",
  rich: "#ffffff",
  bug: "#ffffff",
  "bug-finder": "#ffffff",
  booster: "#ffffff",
  winner: "#ffffff",
  early: "#ffffff",
  founding100: "#ffffff",
  founding1k: "#ffffff",
  "coin-collector": "#f6e6e6",
  stardust: "#fdf6f6",
  "night-owl": "#551212",
  promoter: "#d87b7b",
  creator: "#f4d3d3",
  "discord-staff": "#ffffff",
  "discord-partner": "#e6b0b0",
  "discord-hypesquad": "#ffffff",
  "discord-bug-hunter": "#ffffff",
  "discord-hypesquad-bravery": "#ffffff",
  "discord-hypesquad-brilliance": "#ffffff",
  "discord-hypesquad-balance": "#e6b0b0",
  "discord-early-supporter": "#ffffff",
  "discord-bug-hunter-level-2": "#ffffff",
  "discord-verified-bot": "#ffffff",
  "discord-verified-developer": "#e6b0b0",
  "discord-certified-moderator": "#ffffff",
  "discord-active-developer": "#ffffff",
  "discord-nitro": "#ffffff",
  "discord-server-booster": "#ffffff",
};


function ensureVisibleBadgeColor(c?: string): string {
  if (!c || c === "inherit") return "#000000";
  const hex = c.replace("#", "").trim();
  if (hex.length === 3) {
    const r = parseInt(hex[0] + hex[0], 16);
    const g = parseInt(hex[1] + hex[1], 16);
    const b = parseInt(hex[2] + hex[2], 16);
    const lum = (0.299 * r + 0.587 * g + 0.114 * b) / 255;
    return lum < 0.25 ? "#000000" : c;
  }
  if (hex.length === 6) {
    const r = parseInt(hex.substring(0, 2), 16);
    const g = parseInt(hex.substring(2, 4), 16);
    const b = parseInt(hex.substring(4, 6), 16);
    const lum = (0.299 * r + 0.587 * g + 0.114 * b) / 255;
    return lum < 0.25 ? "#000000" : c;
  }
  return c;
}

export default function BadgeIcon({
  badge,
  monochrome = true,
  color = "#000000",
  glow = false,
  size = 18,
}: {
  badge: BadgeItem;
  monochrome?: boolean;
  color?: string;
  glow?: boolean;
  size?: number;
}) {
  const isCustom = !!badge.custom || badge.id?.startsWith("custom-") || badge.id?.startsWith("creator-") || Boolean(badge.icon && !ICONS[badge.id]);

  const tooltip = badge.name || badge.id || "Badge";

  // Any badge with an image_url renders as an image (e.g. the admin-only
  // Clown badge), regardless of whether it is a custom badge.
  if (badge.image_url) {
    return (
      // eslint-disable-next-line @next/next/no-img-element
      <img
        src={badge.image_url}
        alt={tooltip}
        title={tooltip}
        style={{
          width: size,
          height: size,
          objectFit: "cover",
          borderRadius: Math.max(4, Math.round(size * 0.25)),
          filter: glow ? `drop-shadow(0 0 7px ${color})` : "none",
        }}
      />
    );
  }

  if (isCustom) {
    return (
      <span
        title={tooltip}
        aria-label={tooltip}
        role="img"
        style={{ fontSize: size, lineHeight: 1 }}
      >
        {badge.icon || "⭐"}
      </span>
    );
  }

  const Icon = ICONS[badge.id] || BadgeCheck;
  const rawColor = monochrome ? color : (COLORS[badge.id] || color);
  const iconColor = ensureVisibleBadgeColor(rawColor);

  return (
    <span
      title={tooltip}
      aria-label={tooltip}
      style={{ display: "inline-flex", lineHeight: 1, flex: "none" }}
    >
      <Icon
        size={size}
        strokeWidth={2.25}
        color={iconColor}
        style={{
          filter: glow ? `drop-shadow(0 0 7px ${iconColor})` : "none",
          flex: "none",
        }}
      />
    </span>
  );
}
