"use client";

import Link from "next/link";
import { usePathname } from "next/navigation";
import { cn } from "@/lib/utils";

interface SidebarMenuItemProps {
  Icon: React.ComponentType<React.SVGProps<SVGSVGElement>>;
  title: string;
  href: string;
  isSidebarOpen: boolean;
  onClick?: () => void;
}

export default function SidebarMenuItem({
  Icon,
  title,
  href,
  isSidebarOpen,
  onClick,
}: SidebarMenuItemProps) {
  const pathname = usePathname();

  const isActive =
    href === "/admin"
      ? pathname === "/admin"
      : pathname === href || pathname.startsWith(href + "/");

  return (
    <div className="relative group">
      <Link
        href={href}
        onClick={onClick}
        className={cn(
          "w-full flex items-center gap-2.5 px-3 py-2 rounded-lg text-[13px] font-medium transition-colors duration-150",
          isSidebarOpen ? "justify-start" : "justify-center",
          isActive
            ? "bg-rose-50 text-rose-700"
            : "text-gray-500 hover:text-gray-900 hover:bg-gray-100"
        )}
      >
        {isActive && (
          <span className="absolute left-0 top-1/2 -translate-y-1/2 w-[3px] h-4 rounded-r-full bg-rose-500" />
        )}

        <Icon
          className={cn(
            "w-[17px] h-[17px] flex-shrink-0",
            isActive ? "text-rose-600" : "text-gray-400 group-hover:text-gray-600"
          )}
        />

        {isSidebarOpen && (
          <span className="truncate">{title}</span>
        )}
      </Link>

      {/* Tooltip when collapsed */}
      {!isSidebarOpen && (
        <div className="absolute left-full top-1/2 -translate-y-1/2 ml-3 px-2.5 py-1.5 bg-gray-900 text-white text-xs font-medium rounded-md opacity-0 pointer-events-none group-hover:opacity-100 transition-opacity duration-150 z-50 whitespace-nowrap shadow-lg">
          {title}
        </div>
      )}
    </div>
  );
}
