import { Link, usePage } from '@inertiajs/react';
import { BookOpen, Cpu, FileWarning, FolderGit2, Inbox, LayoutGrid, Shield } from 'lucide-react';
import DiagnosticsController from '@/actions/App/Http/Controllers/Admin/DiagnosticsController';
import SensorTypeController from '@/actions/App/Http/Controllers/Admin/SensorTypeController';
import UserManagementController from '@/actions/App/Http/Controllers/Admin/UserManagementController';
import WebhookMessageController from '@/actions/App/Http/Controllers/Admin/WebhookMessageController';
import AppLogo from '@/components/app-logo';
import { NavFooter } from '@/components/nav-footer';
import { NavMain } from '@/components/nav-main';
import { NavUser } from '@/components/nav-user';
import {
    Sidebar,
    SidebarContent,
    SidebarFooter,
    SidebarHeader,
    SidebarMenu,
    SidebarMenuButton,
    SidebarMenuItem,
} from '@/components/ui/sidebar';
import { dashboard } from '@/routes';
import type { NavItem } from '@/types';

const footerNavItems: NavItem[] = [
    
];

export function AppSidebar() {
    const { auth } = usePage().props as {
        auth: {
            user?: {
                role?: string;
                can_access_admin?: boolean;
                is_admin?: boolean;
            } | null;
        };
    };

    const mainNavItems: NavItem[] = [
        {
            title: 'Dashboard',
            href: dashboard(),
            icon: LayoutGrid,
        },
    ];

    if (auth.user?.can_access_admin) {
        mainNavItems.push(
            {
                title: 'Admin users',
                href: UserManagementController.index(),
                icon: Shield,
            },
            {
                title: 'Sensor types',
                href: SensorTypeController.index(),
                icon: Cpu,
            },
        );

        if (auth.user.is_admin) {
            mainNavItems.push({
                title: 'Incoming messages',
                href: WebhookMessageController.index(),
                icon: Inbox,
            }, {
                title: 'Logs & audit',
                href: DiagnosticsController(),
                icon: FileWarning,
            });
        }
    }

    return (
        <Sidebar collapsible="icon" variant="inset">
            <SidebarHeader>
                <SidebarMenu>
                    <SidebarMenuItem>
                        <SidebarMenuButton size="lg" asChild>
                            <Link href={dashboard()} prefetch>
                                <AppLogo />
                            </Link>
                        </SidebarMenuButton>
                    </SidebarMenuItem>
                </SidebarMenu>
            </SidebarHeader>

            <SidebarContent>
                <NavMain items={mainNavItems} />
            </SidebarContent>

            <SidebarFooter>
                <NavFooter items={footerNavItems} className="mt-auto" />
                <NavUser />
            </SidebarFooter>
        </Sidebar>
    );
}
