import { Link } from '@inertiajs/react';
import {
    SidebarGroup,
    SidebarGroupLabel,
    SidebarMenu,
    SidebarMenuBadge,
    SidebarMenuButton,
    SidebarMenuItem,
} from '@/components/ui/sidebar';
import { useCurrentUrl } from '@/hooks/use-current-url';
import { cn } from '@/lib/utils';
import type { NavItem } from '@/types';

const itemClasses =
    'h-10 gap-3 rounded-lg px-3 text-[14px] font-medium text-sidebar-foreground transition-colors hover:bg-sidebar-accent hover:text-white data-[active=true]:bg-highlight/15 data-[active=true]:font-semibold data-[active=true]:text-[#5eead4] [&>svg]:size-[18px] data-[active=true]:[&>svg]:text-[#5eead4]';

export function NavMain({
    items,
    label,
}: {
    items: NavItem[];
    label?: string;
}) {
    const { isCurrentUrl, isCurrentOrParentUrl } = useCurrentUrl();

    return (
        <SidebarGroup className="px-3 py-1.5">
            {label && (
                <SidebarGroupLabel className="h-7 px-3 text-[11px] font-semibold tracking-[0.12em] text-white/40 uppercase">
                    {label}
                </SidebarGroupLabel>
            )}
            <SidebarMenu className="gap-0.5">
                {items.map((item) => (
                    <SidebarMenuItem key={item.title}>
                        {item.soon ? (
                            <SidebarMenuButton
                                aria-disabled="true"
                                tooltip={{
                                    children: `${item.title} (coming soon)`,
                                }}
                                className={cn(
                                    itemClasses,
                                    'cursor-default text-sidebar-foreground/60 hover:bg-transparent hover:text-sidebar-foreground/60',
                                )}
                            >
                                {item.icon && <item.icon />}
                                <span className="truncate">{item.title}</span>
                                <span className="ml-auto shrink-0 rounded-full bg-white/10 px-2 py-0.5 text-[10px] font-semibold tracking-wide text-white/60 group-data-[collapsible=icon]:hidden">
                                    Soon
                                </span>
                            </SidebarMenuButton>
                        ) : (
                            <SidebarMenuButton
                                asChild
                                isActive={
                                    (item.exact
                                        ? isCurrentUrl(item.href)
                                        : isCurrentOrParentUrl(item.href)) &&
                                    !item.excludes?.some((href) =>
                                        isCurrentOrParentUrl(href),
                                    )
                                }
                                tooltip={{ children: item.title }}
                                className={itemClasses}
                            >
                                <Link href={item.href} prefetch>
                                    {item.icon && <item.icon />}
                                    <span className="truncate">
                                        {item.title}
                                    </span>
                                </Link>
                            </SidebarMenuButton>
                        )}
                        {item.badge ? (
                            <SidebarMenuBadge className="top-2.5 right-2 rounded-full bg-highlight px-1.5 text-white peer-hover/menu-button:text-white peer-data-[active=true]/menu-button:text-white">
                                {item.badge > 99 ? '99+' : item.badge}
                                <span className="sr-only"> unread</span>
                            </SidebarMenuBadge>
                        ) : null}
                    </SidebarMenuItem>
                ))}
            </SidebarMenu>
        </SidebarGroup>
    );
}
