import { Link, useLocation } from "react-router-dom";
import { useTranslation } from "react-i18next";
import { Headset, Monitor, Bot, Sparkles, Video, Wrench, Tag } from "lucide-react";
import { cn } from "@/lib/utils";
import { getLocalizedPath } from "@/lib/routes";

interface DropdownLink {
  name: string;
  href: string;
  icon: React.ElementType;
}

export type NavCategory = "vr" | "ai" | "hologram";

function useDropdownLinks(category: NavCategory, isSwedish: boolean): DropdownLink[] {
  const lang = isSwedish ? "sv" : "en";

  const map: Record<NavCategory, DropdownLink[]> = {
    ai: [
      { name: isSwedish ? "AI Plattform & Lösningar" : "AI Platform & Solutions", href: getLocalizedPath("aiSolutions", lang), icon: Bot },
      { name: "AI Reception & Concierge", href: getLocalizedPath("aiReception", lang), icon: Bot },
      { name: isSwedish ? "AI Automation & Enterprise" : "AI Automation & Enterprise Intelligence", href: getLocalizedPath("aiAutomation", lang), icon: Bot },
      { name: "AI-driven VR Training", href: getLocalizedPath("aiVrTraining", lang), icon: Headset },
    ],
    vr: [
      { name: isSwedish ? "VR Träning & Simulering" : "VR Training & Simulation", href: getLocalizedPath("vrTraining", lang), icon: Headset },
      { name: isSwedish ? "VR Showroom" : "VR Showrooms", href: getLocalizedPath("vrShowroom", lang), icon: Monitor },
      { name: isSwedish ? "VR Upplevelser & Interaktiva miljöer" : "VR Experiences & Interactive Environments", href: getLocalizedPath("vrExperiences", lang), icon: Sparkles },
    ],
    hologram: [
      { name: isSwedish ? "Hologramlösningar" : "Holographic Solutions", href: getLocalizedPath("hologramSolutions", lang), icon: Monitor },
      { name: isSwedish ? "Hologramdisplayer" : "Hologram Displays", href: getLocalizedPath("hologramTechnology", lang), icon: Wrench },
      { name: isSwedish ? "Hologramproduktion" : "Hologram Production", href: getLocalizedPath("hologramProduction", lang), icon: Video },
      { name: isSwedish ? "Hyr hologram" : "Rent a Hologram", href: getLocalizedPath("hyrHologram", lang), icon: Tag },
    ],
  };

  return map[category];
}

interface DropdownProps {
  category: NavCategory;
  onLinkClick?: () => void;
}

/** Desktop dropdown for a single category */
export function CategoryDropdown({ category, onLinkClick }: DropdownProps) {
  const { i18n } = useTranslation();
  const isSwedish = i18n.language === "sv";
  const location = useLocation();
  const links = useDropdownLinks(category, isSwedish);

  return (
    <div className="py-3 px-2 min-w-[280px]">
      <ul className="space-y-0.5">
        {links.map((link) => {
          const Icon = link.icon;
          const active = location.pathname === link.href;
          return (
            <li key={link.href}>
              <Link
                to={link.href}
                onClick={onLinkClick}
                className={cn(
                  "flex items-center gap-2.5 rounded-lg px-3 py-2.5 text-[13px] font-medium transition-all duration-150",
                  active
                    ? "bg-[#2563EB]/12 text-white"
                    : "text-white/70 hover:text-white hover:bg-white/[0.04]"
                )}
              >
                <Icon className="h-4 w-4 shrink-0" style={{ color: active ? '#2563EB' : 'rgba(37,99,235,0.5)' }} />
                <span>{link.name}</span>
              </Link>
            </li>
          );
        })}
      </ul>
    </div>
  );
}

/** Mobile accordion for a single category */
export function CategoryMobileAccordion({ category, onLinkClick }: DropdownProps) {
  const { i18n } = useTranslation();
  const isSwedish = i18n.language === "sv";
  const location = useLocation();
  const links = useDropdownLinks(category, isSwedish);

  return (
    <ul className="pl-3 border-l border-white/10 ml-1 space-y-0.5 pb-1">
      {links.map((link) => {
        const Icon = link.icon;
        const active = location.pathname === link.href;
        return (
          <li key={link.href}>
            <Link
              to={link.href}
              onClick={onLinkClick}
              className={cn(
                "flex items-center gap-2 py-2 px-2 rounded text-sm transition-colors",
                active ? "text-white bg-white/[0.06]" : "text-white/50 hover:text-white/80"
              )}
            >
              <Icon className="h-3.5 w-3.5 shrink-0 opacity-50" />
              {link.name}
            </Link>
          </li>
        );
      })}
    </ul>
  );
}
