import { Link } from '@inertiajs/react';
import type { PropsWithChildren } from 'react';
import Heading from '@/components/heading';
import { Button } from '@/components/ui/button';
import { Separator } from '@/components/ui/separator';
import { useCurrentUrl } from '@/hooks/use-current-url';
import { cn, toUrl } from '@/lib/utils';
import { edit as editIdentity } from '@/routes/admin/settings/company';
import { edit as editGeneral } from '@/routes/admin/settings/company/general';
import { edit as editContact } from '@/routes/admin/settings/company/contact';
import { edit as editFiscal } from '@/routes/admin/settings/company/fiscal';
import { edit as editNumbering } from '@/routes/admin/settings/company/numbering';
import { edit as editSecurity } from '@/routes/admin/settings/company/security';
import type { NavItem } from '@/types';

const sidebarNavItems: NavItem[] = [
    {
        title: 'Identité',
        href: editIdentity(),
        icon: null,
    },
    {
        title: 'Général',
        href: editGeneral(),
        icon: null,
    },
    {
        title: 'Contact',
        href: editContact(),
        icon: null,
    },
    {
        title: 'Fiscalité',
        href: editFiscal(),
        icon: null,
    },
    {
        title: 'Numérotation',
        href: editNumbering(),
        icon: null,
    },
    {
        title: 'Sécurité',
        href: editSecurity(),
        icon: null,
    },
];

export default function AdminSettingsLayout({ children }: PropsWithChildren) {
    const { isCurrentUrl } = useCurrentUrl();

    return (
        <div className="px-4 py-6">
            <Heading
                title="Paramètres de l'entreprise"
                description="Configuration générale de l'application"
            />

            <div className="flex flex-col lg:flex-row lg:space-x-12">
                <aside className="w-full max-w-xl lg:w-48">
                    <nav
                        className="flex flex-col space-y-1 space-x-0"
                        aria-label="Paramètres de l'entreprise"
                    >
                        {sidebarNavItems.map((item, index) => (
                            <Button
                                key={`${toUrl(item.href)}-${index}`}
                                size="sm"
                                variant="ghost"
                                asChild
                                className={cn('w-full justify-start', {
                                    'bg-muted': isCurrentUrl(item.href),
                                })}
                            >
                                <Link href={item.href}>
                                    {item.icon && (
                                        <item.icon className="h-4 w-4" />
                                    )}
                                    {item.title}
                                </Link>
                            </Button>
                        ))}
                    </nav>
                </aside>

                <Separator className="my-6 lg:hidden" />

                <div className="flex-1 md:max-w-2xl">
                    <section className="max-w-xl space-y-12">
                        {children}
                    </section>
                </div>
            </div>
        </div>
    );
}
