import { Head, Link, usePage } from '@inertiajs/react';
import type { PropsWithChildren } from 'react';

import { ThemeToggle } from '@/components/theme/theme-toggle';
import { cn } from '@/lib/utils';
import type { AppPageProps } from '@/types';

type PublicLayoutProps = PropsWithChildren<{
    title?: string;
}>;

type HeaderActionProps = {
    href: string;
    label: string;
    icon: IconName;
    variant?: 'default' | 'primary' | 'admin';
    count?: number;
    method?: 'post';
    as?: 'button';
};

type IconName =
    | 'admin'
    | 'blog'
    | 'cart'
    | 'catalog'
    | 'login'
    | 'logout'
    | 'profile'
    | 'register';

export function PublicLayout({ children, title }: PublicLayoutProps) {
    const { auth, cart } = usePage<AppPageProps>().props;
    const isAdmin =
        auth.user?.roles.some((role) => role.slug === 'admin') ?? false;

    return (
        <>
            <Head title={title} />
            <div className="min-h-screen bg-background text-main">
                <header className="border-b border-border bg-card/95 backdrop-blur">
                    <div className="mx-auto flex w-full max-w-7xl flex-col gap-3 px-4 py-3 sm:flex-row sm:items-center sm:justify-between sm:px-6 lg:px-8">
                        <Link
                            href="/"
                            className="text-lg font-semibold tracking-tight text-main"
                        >
                            Parts Platform
                        </Link>
                        <div className="flex w-full flex-col gap-3 sm:w-auto sm:flex-row sm:items-center">
                            <ThemeToggle />
                            <nav className="grid w-full grid-cols-4 gap-2 sm:flex sm:w-auto sm:flex-wrap sm:justify-end">
                                <HeaderAction
                                    href="/catalog"
                                    label="Catalog"
                                    icon="catalog"
                                />
                                <HeaderAction
                                    href="/blog"
                                    label="Blog"
                                    icon="blog"
                                />
                                <HeaderAction
                                    href="/cart"
                                    label="Cart"
                                    icon="cart"
                                    count={cart.item_count}
                                />
                                {auth.user ? (
                                    <>
                                        <HeaderAction
                                            href="/account/orders"
                                            label="Account"
                                            icon="profile"
                                        />
                                        {isAdmin ? (
                                            <HeaderAction
                                                href="/admin/dashboard"
                                                label="Admin"
                                                icon="admin"
                                                variant="admin"
                                            />
                                        ) : null}
                                        <HeaderAction
                                            href="/logout"
                                            label="Logout"
                                            icon="logout"
                                            method="post"
                                            as="button"
                                            variant="primary"
                                        />
                                    </>
                                ) : (
                                    <>
                                        <HeaderAction
                                            href="/login"
                                            label="Login"
                                            icon="login"
                                        />
                                        <HeaderAction
                                            href="/register"
                                            label="Register"
                                            icon="register"
                                            variant="primary"
                                        />
                                    </>
                                )}
                            </nav>
                        </div>
                    </div>
                </header>
                <main className="mx-auto w-full max-w-7xl px-4 py-8 sm:px-6 lg:px-8">
                    {children}
                </main>
            </div>
        </>
    );
}

function HeaderAction({
    href,
    label,
    icon,
    variant = 'default',
    count,
    method,
    as,
}: HeaderActionProps) {
    return (
        <Link
            href={href}
            method={method}
            as={as}
            aria-label={label}
            className={cn(
                'relative inline-flex min-h-12 items-center justify-center gap-1.5 rounded-xl border px-2 py-2 text-xs font-semibold transition sm:min-h-10 sm:flex-row sm:rounded-lg sm:px-3 sm:text-sm',
                variant === 'default' &&
                    'border-border bg-card text-muted hover:bg-surface hover:text-main',
                variant === 'admin' &&
                    'border-primary bg-primary/10 text-primary hover:bg-primary/15',
                variant === 'primary' &&
                    'border-primary bg-primary text-white hover:bg-primary-hover',
            )}
        >
            <NavIcon name={icon} />
            <span className="leading-none">{label}</span>
            {typeof count === 'number' ? (
                <span className="absolute -top-1 -right-1 min-w-5 rounded-full bg-primary px-1.5 py-0.5 text-center text-[10px] leading-none font-bold text-white ring-2 ring-card">
                    {count}
                </span>
            ) : null}
        </Link>
    );
}

function NavIcon({ name }: { name: IconName }) {
    const common = {
        className: 'h-4 w-4 shrink-0',
        fill: 'none',
        stroke: 'currentColor',
        strokeLinecap: 'round' as const,
        strokeLinejoin: 'round' as const,
        strokeWidth: 2,
        viewBox: '0 0 24 24',
        'aria-hidden': true,
    };

    if (name === 'catalog') {
        return (
            <svg {...common}>
                <path d="M4 5h16" />
                <path d="M4 12h16" />
                <path d="M4 19h16" />
                <path d="M8 5v14" />
            </svg>
        );
    }

    if (name === 'blog') {
        return (
            <svg {...common}>
                <path d="M5 4h11a3 3 0 0 1 3 3v13H8a3 3 0 0 0-3 3V4Z" />
                <path d="M8 8h7" />
                <path d="M8 12h6" />
            </svg>
        );
    }

    if (name === 'cart') {
        return (
            <svg {...common}>
                <path d="M4 5h2l2.2 10.2a2 2 0 0 0 2 1.6h6.9a2 2 0 0 0 1.9-1.4L21 8H7" />
                <path d="M10 21h.01" />
                <path d="M18 21h.01" />
            </svg>
        );
    }

    if (name === 'profile') {
        return (
            <svg {...common}>
                <path d="M12 12a4 4 0 1 0 0-8 4 4 0 0 0 0 8Z" />
                <path d="M4 21a8 8 0 0 1 16 0" />
            </svg>
        );
    }

    if (name === 'admin') {
        return (
            <svg {...common}>
                <path d="M12 3 20 7v5c0 5-3.4 8-8 9-4.6-1-8-4-8-9V7l8-4Z" />
                <path d="M9 12l2 2 4-4" />
            </svg>
        );
    }

    if (name === 'logout') {
        return (
            <svg {...common}>
                <path d="M10 17 15 12 10 7" />
                <path d="M15 12H3" />
                <path d="M21 5v14" />
            </svg>
        );
    }

    if (name === 'register') {
        return (
            <svg {...common}>
                <path d="M15 19a6 6 0 0 0-12 0" />
                <path d="M9 11a4 4 0 1 0 0-8 4 4 0 0 0 0 8Z" />
                <path d="M19 8v6" />
                <path d="M16 11h6" />
            </svg>
        );
    }

    return (
        <svg {...common}>
            <path d="M15 3h4a2 2 0 0 1 2 2v14a2 2 0 0 1-2 2h-4" />
            <path d="M10 17 15 12 10 7" />
            <path d="M15 12H3" />
        </svg>
    );
}
