"use client";

import { ChevronRight } from "lucide-react";

import {
  Collapsible,
  CollapsibleContent,
  CollapsibleTrigger,
} from "@/app/_components/ui/collapsible";
import {
  SidebarGroup,
  SidebarGroupLabel,
  SidebarMenu,
  SidebarMenuButton,
  SidebarMenuItem,
  SidebarMenuSub,
} from "@/app/_components/ui/sidebar";

import * as TablerIcons from "@tabler/icons-react";
import { useNavigations } from "@/app/_hooks/use-navigations";

interface SubSubItem {
  title: string;
  url: string;
}

interface SubItem {
  title: string;
  url: string;
  icon?: TablerIcons.Icon;
  subSubItems?: SubSubItem[];
}

interface NavItem {
  title: string;
  url: string;
  icon?: TablerIcons.Icon;
  isActive?: boolean;
  subItems?: SubItem[];
}

function SubSubItemComponent({ item }: { item: SubSubItem }) {
  const router = useNavigations();
  const isActive = router.pathname === item.url;

  return (
    <SidebarMenuItem className={isActive ? "active text-primary" : ""}>
      <SidebarMenuButton asChild>
        <a href={item.url}>
          <span>{item.title}</span>
        </a>
      </SidebarMenuButton>
    </SidebarMenuItem>
  );
}

function SubItemComponent({ item }: { item: SubItem }) {
  const router = useNavigations();
  const isActive = router.pathname === item.url;
  const hasSubSubItems = item.subSubItems && item.subSubItems.length > 0;

  if (!hasSubSubItems) {
    return (
      <SidebarMenuItem className={isActive ? "active text-primary" : ""}>
        <SidebarMenuButton asChild>
          <a href={item.url}>
            {item.icon && <item.icon />}
            <span>{item.title}</span>
          </a>
        </SidebarMenuButton>
      </SidebarMenuItem>
    );
  }

  return (
    <Collapsible asChild className="group/collapsible">
      <SidebarMenuItem className={isActive ? "active text-primary" : ""}>
        <CollapsibleTrigger asChild>
          <SidebarMenuButton>
            {item.icon && <item.icon />}
            <span>{item.title}</span>
            <ChevronRight className="ml-auto transition-transform duration-200 group-data-[state=open]/collapsible:rotate-90" />
          </SidebarMenuButton>
        </CollapsibleTrigger>
        <CollapsibleContent>
          <SidebarMenuSub>
            {item.subSubItems!.map((subSubItem) => (
              <SubSubItemComponent key={subSubItem.title} item={subSubItem} />
            ))}
          </SidebarMenuSub>
        </CollapsibleContent>
      </SidebarMenuItem>
    </Collapsible>
  );
}

function RecursiveNavItem({ item, index }: { item: NavItem; index: number }) {
  const router = useNavigations();
  const isActive = router.pathname === item.url;
  const hasSubItems = item.subItems && item.subItems.length > 0;

  if (!hasSubItems) {
    return (
      <SidebarMenuItem className={isActive ? "active text-primary" : ""}>
        <SidebarMenuButton tooltip={item.title} asChild>
          <a href={item.url}>
            {item.icon && <item.icon />}
            <span>{item.title}</span>
          </a>
        </SidebarMenuButton>
      </SidebarMenuItem>
    );
  }

  return (
    <Collapsible
      key={item.title}
      asChild
      defaultOpen={index === 1}
      className="group/collapsible"
    >
      <SidebarMenuItem className={isActive ? "active text-primary" : ""}>
        <CollapsibleTrigger asChild>
          <SidebarMenuButton tooltip={item.title}>
            {item.icon && <item.icon />}
            <span>{item.title}</span>
            {hasSubItems && (
              <ChevronRight className="ml-auto transition-transform duration-200 group-data-[state=open]/collapsible:rotate-90" />
            )}
          </SidebarMenuButton>
        </CollapsibleTrigger>
        <CollapsibleContent>
          <SidebarMenuSub>
            {item.subItems!.map((subItem) => (
              <SubItemComponent key={subItem.title} item={subItem} />
            ))}
          </SidebarMenuSub>
        </CollapsibleContent>
      </SidebarMenuItem>
    </Collapsible>
  );
}

export function NavMain({ items }: { items: NavItem[] }) {
  return (
    <SidebarGroup>
      <SidebarGroupLabel>Platform</SidebarGroupLabel>
      <SidebarMenu>
        {items.map((item, index) => (
          <RecursiveNavItem key={item.title} item={item} index={index} />
        ))}
      </SidebarMenu>
    </SidebarGroup>
  );
}
