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 { SharedData, type NavItem } from '@/types';
import { Link, usePage } from '@inertiajs/react';
import {
    AlignVerticalDistributeCenter,
    Book,
    Briefcase,
    Calendar,
    Database,
    Download,
    FileText,
    LayoutGrid,
    List,
    Shield,
    Upload,
    UserCheck,
} from 'lucide-react';
import AppLogo from './app-logo';
import { NavDashboard } from './nav-dashboard';
import { NavMainDua } from './nav-main-dua';
import { NavMainEmpat } from './nav-main-empat';
import { NavMainTiga } from './nav-main-tiga';

export function AppSidebar() {
    const { auth } = usePage<SharedData>().props;
    let dashboardHref = '#';

    if (auth.user.role === 'super admin') {
        dashboardHref = '/dashboard/super';
    } else if (auth.user.role === 'administrator') {
        dashboardHref = '/dashboard/administrator';
    } else if (auth.user.role === 'operator') {
        dashboardHref = '/dashboard/operator';
    }

    let dashboardNavItems: NavItem[] = [
        {
            title: 'Dashboard',
            href: dashboardHref,
            icon: LayoutGrid,
        },
    ];
    let mainSakipNavItems: NavItem[] = [];
    let mainNavItems: NavItem[] = [];
    let lainnyaNavItems: NavItem[] = [];
    let pengaturanNavItems: NavItem[] = [];

    if (auth.user.role === 'super admin') {
        mainNavItems.push(
            {
                title: 'Program',
                href: '/programs',
                icon: Book,
            },
            {
                title: 'Kegiatan',
                href: '/kegiatans',
                icon: List,
            },
            {
                title: 'Sub Kegiatan',
                href: '/sub-kegiatans',
                icon: FileText,
            },
            {
                title: 'Rekening',
                href: '/rekenings',
                icon: Briefcase,
            },
        );
        lainnyaNavItems.push(
            {
                title: 'Tahun',
                href: '/tahuns',
                icon: Calendar,
            },
            {
                title: 'Hak Akses',
                href: '/hakakses',
                icon: Shield,
            },
            {
                title: 'ASN',
                href: '/pegawais',
                icon: UserCheck,
            },
        );
        pengaturanNavItems.push(
            {
                title: 'Pengguna',
                href: '/users',
                icon: UserCheck,
            },
            {
                title: 'Copy Database',
                href: '/copydb',
                icon: Database,
            },
            {
                title: 'Import',
                href: '/import',
                icon: Upload,
            },
            {
                title: 'Export',
                href: '/export',
                icon: Download,
            },
        );
    } else if (auth.user.role === 'administrator') {
        mainSakipNavItems.push({
            title: 'Sakip',
            href: '#',
            icon: AlignVerticalDistributeCenter,
            isActive: true,
            children: [
                {
                    title: 'Kertas Kerja',
                    href: '/sakip',
                },
                {
                    title: 'Cascading',
                    href: '/cascading',
                },
                {
                    title: 'Pohon Kinerja Teknis',
                    href: '/sakip/pohon-kinerja',
                },
                {
                    title: 'Pohon Kinerja Rutin',
                    href: '/sakip/pohon-kinerja-rutin',
                },
                {
                    title: 'Perjanjian Kinerja',
                    href: '/pk',
                },
                {
                    title: 'IKU',
                    href: '/a/iku',
                },
                {
                    title: 'IKI',
                    href: '/a/iki',
                },
            ],
        });
    } else {
    }

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

            <SidebarContent>
                <NavDashboard items={dashboardNavItems} />
                {auth.user.role === 'administrator' && (
                    <NavMainDua items={mainSakipNavItems} />
                )}
                {auth.user.role === 'super admin' && (
                    <div>
                        <NavMain items={mainNavItems} />
                        <NavMainTiga items={lainnyaNavItems} />
                        <NavMainEmpat items={pengaturanNavItems} />
                    </div>
                )}
            </SidebarContent>

            <SidebarFooter>
                <NavUser />
            </SidebarFooter>
        </Sidebar>
    );
}
