import { Link } from '@inertiajs/react';
import type { ComponentPropsWithoutRef } from 'react';
import { useState } from 'react';
import {
    SidebarGroup,
    SidebarGroupContent,
    SidebarMenu,
    SidebarMenuButton,
    SidebarMenuItem,
    SidebarMenuSub,
    SidebarMenuSubButton,
    SidebarMenuSubItem,
} from '@/components/ui/sidebar';
import { useCurrentUrl } from '@/hooks/use-current-url';
import { toUrl } from '@/lib/utils';
import type { NavItem } from '@/types';

export function NavFooter({
    items,
    className,
    ...props
}: ComponentPropsWithoutRef<typeof SidebarGroup> & {
    items: NavItem[];
}) {
    const { isCurrentUrl } = useCurrentUrl();
    const [openMenus, setOpenMenus] = useState<Record<string, boolean>>({});

    return (
        <SidebarGroup
            {...props}
            className={`group-data-[collapsible=icon]:p-0 ${className || ''}`}
        >
            <SidebarGroupContent>
                <SidebarMenu>
                    {items.map((item) => {
                        const hasChildren = Boolean(item.children?.length);
                        const isExternal =
                            typeof item.href === 'string' &&
                            /^https?:\/\//i.test(item.href);
                        const isChildActive =
                            item.children?.some((child) =>
                                isCurrentUrl(child.href),
                            ) ?? false;
                        const isOpen = openMenus[item.title] ?? isChildActive;

                        return (
                            <SidebarMenuItem key={item.title}>
                                <SidebarMenuButton
                                    asChild={!hasChildren}
                                    type={hasChildren ? 'button' : undefined}
                                    data-state={isOpen ? 'open' : 'closed'}
                                    onClick={
                                        hasChildren
                                            ? () =>
                                                  setOpenMenus((prev) => ({
                                                      ...prev,
                                                      [item.title]: !isOpen,
                                                  }))
                                            : undefined
                                    }
                                    className="text-neutral-600 hover:text-neutral-800 dark:text-neutral-300 dark:hover:text-neutral-100"
                                >
                                    {hasChildren ? (
                                        <div className="flex items-center gap-2">
                                            {item.icon && (
                                                <item.icon className="h-5 w-5" />
                                            )}
                                            <span>{item.title}</span>
                                        </div>
                                    ) : isExternal ? (
                                        <a
                                            href={toUrl(item.href)}
                                            target="_blank"
                                            rel="noopener noreferrer"
                                        >
                                            {item.icon && (
                                                <item.icon className="h-5 w-5" />
                                            )}
                                            <span>{item.title}</span>
                                        </a>
                                    ) : (
                                        <Link href={item.href}>
                                            {item.icon && (
                                                <item.icon className="h-5 w-5" />
                                            )}
                                            <span>{item.title}</span>
                                        </Link>
                                    )}
                                </SidebarMenuButton>

                                {hasChildren && isOpen ? (
                                    <SidebarMenuSub>
                                        {item.children?.map((child) => (
                                            <SidebarMenuSubItem key={child.title}>
                                                <SidebarMenuSubButton
                                                    asChild
                                                    isActive={isCurrentUrl(
                                                        child.href,
                                                    )}
                                                >
                                                    <Link href={child.href}>
                                                        {child.title}
                                                    </Link>
                                                </SidebarMenuSubButton>
                                            </SidebarMenuSubItem>
                                        ))}
                                    </SidebarMenuSub>
                                ) : null}
                            </SidebarMenuItem>
                        );
                    })}
                </SidebarMenu>
            </SidebarGroupContent>
        </SidebarGroup>
    );
}
