"use client";

import {
  AppWindow, BarChart3, Bell, Bot, Calendar, Car, FileText, Fuel, Gift, Leaf, Lightbulb, Map, MapPin,
  Megaphone, MousePointerClick, PanelLeft, PanelTop, Rocket, ShieldCheck, Sparkles, SquareStack, Star, Tag,
  Users, Wrench, Zap, type LucideIcon,
} from "lucide-react";
import { cn } from "@/shared/lib/utils";
import { useT } from "@/shared/i18n/client";
import { DISPLAY_STATE_CLASSES, FORMAT_CLASSES, FORMAT_ICONS } from "@/shared/constants/announcements";
import type { AnnouncementDisplayState, AnnouncementFormat } from "@/domain/entities/announcement";

const ICONS: Record<string, LucideIcon> = {
  Sparkles, Rocket, Gift, Star, Lightbulb, Zap, Bell, Megaphone, Map, MapPin, Car, Fuel, Wrench,
  ShieldCheck, FileText, BarChart3, Users, Calendar, Bot, Leaf,
  Tag, PanelLeft, PanelTop, AppWindow, SquareStack, MousePointerClick,
};

/** An icon of the fixed list, by name. Unknown names render nothing. */
export function AnnouncementIcon({ name, className }: { name: string | null; className?: string }) {
  const Icon = name ? ICONS[name] : undefined;
  return Icon ? <Icon className={className} aria-hidden="true" /> : null;
}

export function FormatIcon({ format, className }: { format: AnnouncementFormat; className?: string }) {
  return <AnnouncementIcon name={FORMAT_ICONS[format]} className={className} />;
}

/** The format name; a one-step tour reads "Info-bulle sur un élément". */
export function useFormatName() {
  const t = useT();
  return (format: AnnouncementFormat, stepCount = 0) =>
    format === "tour" && stepCount === 1 ? t.announcements.singleStepTour : t.announcements.formats[format].name;
}

export function FormatBadge({ format, stepCount }: { format: AnnouncementFormat; stepCount?: number }) {
  const formatName = useFormatName();
  return (
    <span className={cn("inline-flex items-center gap-1 rounded-full border px-2 py-0.5 text-[11px] font-medium whitespace-nowrap", FORMAT_CLASSES[format])}>
      <FormatIcon format={format} className="h-3 w-3" />
      {formatName(format, stepCount)}
    </span>
  );
}

export function DisplayStateBadge({ state }: { state: AnnouncementDisplayState }) {
  const t = useT();
  return (
    <span className={cn("inline-flex items-center gap-1.5 rounded-full border px-2 py-0.5 text-[11px] font-semibold whitespace-nowrap", DISPLAY_STATE_CLASSES[state])}>
      <span
        className={cn(
          "h-1.5 w-1.5 rounded-full",
          state === "live" ? "bg-emerald-500" : state === "scheduled" ? "bg-blue-500" : state === "ended" ? "bg-amber-500" : "bg-current opacity-50",
        )}
        aria-hidden="true"
      />
      {t.announcements.states[state]}
    </span>
  );
}
