import type { LucideIcon } from "lucide-react";
import {
  BookOpen,
  Bot,
  Braces,
  CalendarCheck,
  CircuitBoard,
  ClipboardCheck,
  Code2,
  Crown,
  FileCode2,
  Footprints,
  GitBranch,
  GraduationCap,
  Lightbulb,
  List,
  Repeat,
  Send,
  Sparkles,
  Star,
  Sword,
  ToggleLeft,
  Trophy,
  Type,
} from "lucide-react";
import { cn } from "@/lib/utils";

/**
 * Icon registry for data-driven glyphs.
 *
 * Skill and achievement rows persist an icon *name* so the taxonomy stays
 * editable without a code change. This map is the allow-list of names that may
 * be resolved — importing the whole `lucide-react` namespace instead would pull
 * all ~6300 glyphs (≈740 KB) into the client bundle for every page that renders
 * one icon. Add a name here when a new skill or achievement needs it; anything
 * unrecognised falls back to a neutral glyph.
 */
const ICON_REGISTRY: Record<string, LucideIcon> = {
  // Skills
  Braces,
  FileCode2,
  List,
  Repeat,
  Type,
  ToggleLeft,
  Lightbulb,
  GitBranch,
  Bot,
  CircuitBoard,
  // Achievements
  Footprints,
  BookOpen,
  GraduationCap,
  Send,
  ClipboardCheck,
  Code2,
  Sword,
  Sparkles,
  Star,
  Crown,
  Trophy,
  CalendarCheck,
};

/** Resolve a stored icon name to a Lucide component. */
export function resolveIcon(name: string | null | undefined): LucideIcon {
  if (!name) return Sparkles;
  return ICON_REGISTRY[name] ?? Sparkles;
}

/** Render a Lucide glyph from a persisted icon name. */
export function DynamicIcon({
  name,
  className,
}: {
  name: string | null | undefined;
  className?: string;
}) {
  const Icon = resolveIcon(name);
  return <Icon className={cn(className)} aria-hidden="true" />;
}
