/* The platform admin portal: businesses and their subscriptions. A sidebar on computers, a top bar on phones. */
import { Head, Link, router, usePage } from '@inertiajs/react';
import type { ComponentType, ReactNode } from 'react';
import { Building2, CreditCard, DatabaseBackup, LayoutDashboard, LogOut, MessageSquareText, UserRound, type LucideProps } from 'lucide-react';
import type { SharedProps } from '../types';
import { cx } from '../components/ui';
import { FlashToast } from '../components/overlays';
import { AppMark } from '../components/brand';

const NAV: { label: string; route: string; match?: string; icon: ComponentType<LucideProps> }[] = [
    { label: 'Overview', route: 'platform.dashboard', icon: LayoutDashboard },
    { label: 'Businesses', route: 'platform.businesses.index', match: 'platform.businesses.*', icon: Building2 },
    { label: 'SMS', route: 'platform.sms', icon: MessageSquareText },
    { label: 'Payment details', route: 'platform.settings', icon: CreditCard },
    { label: 'Backups', route: 'platform.backups.index', icon: DatabaseBackup },
    { label: 'My profile', route: 'platform.profile', icon: UserRound },
];

export default function PlatformLayout({ title, children }: { title: string; children: ReactNode }) {
    const { auth, appName } = usePage<SharedProps>().props;
    const on = (n: (typeof NAV)[number]) => route().current(n.route) || (!!n.match && route().current(n.match));

    return (
        <>
            <Head title={`${title} · ${appName} platform`} />
            <FlashToast />
            <div className="min-h-screen lg:flex">
                <aside className="border-b border-line bg-white lg:sticky lg:top-0 lg:flex lg:h-screen lg:w-60 lg:shrink-0 lg:flex-col lg:border-r lg:border-b-0">
                    <div className="flex items-center gap-2.5 px-4 py-3 lg:h-16 lg:px-5">
                        <AppMark />
                        <div className="min-w-0 flex-1">
                            <p className="text-sm leading-tight font-extrabold text-ink">{appName}</p>
                            <p className="text-xs leading-tight text-muted">Platform admin</p>
                        </div>
                        <button onClick={() => router.post(route('logout'))} className="grid size-9 place-items-center rounded-lg text-muted hover:bg-line-2 lg:hidden" aria-label="Sign out"><LogOut className="size-4" /></button>
                    </div>
                    <nav className="no-scrollbar flex gap-1 overflow-x-auto px-3 pb-2 lg:flex-1 lg:flex-col lg:pb-0" aria-label="Main">
                        {NAV.map((n) => (
                            <Link key={n.route} href={route(n.route)} className={cx('flex shrink-0 items-center gap-2.5 rounded-lg px-3 py-2 text-sm font-semibold', on(n) ? 'bg-brand/10 text-brand' : 'text-ink-2 hover:bg-line-2')}>
                                <n.icon className="size-4" aria-hidden />{n.label}
                            </Link>
                        ))}
                    </nav>
                    <div className="hidden border-t border-line p-3 lg:block">
                        <p className="truncate px-3 text-sm font-semibold text-ink">{auth?.name}</p>
                        <button onClick={() => router.post(route('logout'))} className="mt-1 flex w-full items-center gap-2.5 rounded-lg px-3 py-2 text-sm font-semibold text-ink-2 hover:bg-line-2"><LogOut className="size-4" aria-hidden />Sign out</button>
                    </div>
                </aside>
                <main className="mx-auto w-full max-w-6xl flex-1 px-4 py-6 sm:px-6 lg:px-8 lg:py-8">{children}</main>
            </div>
        </>
    );
}
