import { Link } from '@inertiajs/react';
import { HandCoins, LayoutGrid, Menu, PiggyBank, Wallet } from 'lucide-react';
import type { LucideIcon } from 'lucide-react';
import { useSidebar } from '@/components/ui/sidebar';
import { useCurrentUrl } from '@/hooks/use-current-url';
import { cn } from '@/lib/utils';
import { dashboard } from '@/routes';
import { index as financingIndex } from '@/routes/financing';
import { index as savingsIndex } from '@/routes/savings';
import { show as walletShow } from '@/routes/wallet';

type Tab = {
    label: string;
    href: ReturnType<typeof dashboard>;
    icon: LucideIcon;
};

const sideTabs: [Tab, Tab, Tab, Tab] = [
    { label: 'Home', href: dashboard(), icon: LayoutGrid },
    { label: 'Savings', href: savingsIndex(), icon: PiggyBank },
    { label: 'Financing', href: financingIndex(), icon: HandCoins },
    { label: 'Wallet', href: walletShow(), icon: Wallet },
];

const tabClasses =
    'flex flex-1 flex-col items-center justify-center gap-0.5 py-2 text-[11px] font-medium transition-colors focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-highlight focus-visible:ring-inset';

/**
 * Bottom navigation for phones. The centre button is the wallet; "Menu"
 * opens the full sidebar for everything else.
 */
export function MobileTabBar() {
    const { isCurrentOrParentUrl } = useCurrentUrl();
    const { setOpenMobile } = useSidebar();
    const [home, savings, financing, wallet] = sideTabs;

    const renderTab = (tab: Tab) => {
        const isActive = isCurrentOrParentUrl(tab.href);

        return (
            <Link
                key={tab.label}
                href={tab.href}
                aria-current={isActive ? 'page' : undefined}
                className={cn(
                    tabClasses,
                    isActive ? 'text-brand' : 'text-ink-400 hover:text-ink-700',
                )}
            >
                <tab.icon
                    className={cn('size-5', isActive && 'text-highlight-dark')}
                />
                <span className={cn(isActive && 'font-semibold')}>
                    {tab.label}
                </span>
            </Link>
        );
    };

    const isWalletActive = isCurrentOrParentUrl(wallet.href);

    return (
        <nav
            aria-label="Quick navigation"
            className="print-hidden fixed inset-x-0 bottom-0 z-30 border-t border-surface-200 bg-white/95 pb-[env(safe-area-inset-bottom)] shadow-[0_-4px_20px_rgb(15_27_45/0.06)] backdrop-blur md:hidden"
        >
            <div className="mx-auto flex max-w-md items-stretch">
                {renderTab(home)}
                {renderTab(savings)}
                <Link
                    href={wallet.href}
                    aria-current={isWalletActive ? 'page' : undefined}
                    className={cn(
                        tabClasses,
                        isWalletActive ? 'text-brand' : 'text-ink-400',
                    )}
                >
                    <span className="-mt-5 flex size-12 items-center justify-center rounded-full bg-brand text-white shadow-landing ring-4 ring-white">
                        <wallet.icon className="size-5" />
                    </span>
                    <span className={cn(isWalletActive && 'font-semibold')}>
                        {wallet.label}
                    </span>
                </Link>
                {renderTab(financing)}
                <button
                    type="button"
                    onClick={() => setOpenMobile(true)}
                    className={cn(
                        tabClasses,
                        'text-ink-400 hover:text-ink-700',
                    )}
                >
                    <Menu className="size-5" />
                    <span>Menu</span>
                </button>
            </div>
        </nav>
    );
}
