import { Link } from '@inertiajs/react';
import {
    BookOpen,
    Building2,
    FileText,
    GraduationCap,
    HeartHandshake,
    LayoutGrid,
    Receipt,
    Settings,
    Users,
} from 'lucide-react';
import AppLogo from '@/components/app-logo';
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 { useI18n } from '@/hooks/use-i18n';
import { dashboard } from '@/routes';
import { index as academicIndex } from '@/routes/academic';
import { index as promotionIndex } from '@/routes/academic/promotion';
import { index as attendanceIndex } from '@/routes/attendance';
import { index as chandaIndex } from '@/routes/chanda';
import { index as examsIndex } from '@/routes/exams';
import { index as expensesIndex } from '@/routes/expenses';
import { index as feesIndex } from '@/routes/fees';
import { index as invoicesIndex } from '@/routes/invoices';
import { edit as editProfile } from '@/routes/profile';
import { dues as duesReport } from '@/routes/reports';
import { index as studentsIndex } from '@/routes/students';
import { index as institutionsIndex } from '@/routes/system/institutions';
import { index as settingsIndex } from '@/routes/system/settings';
import { index as usersIndex } from '@/routes/system/users';
import type { NavItem } from '@/types';

export function AppSidebar() {
    const { t } = useI18n();

    const overviewNavItems: NavItem[] = [
        {
            title: t('nav.dashboard', 'Dashboard'),
            href: dashboard(),
            icon: LayoutGrid,
        },
    ];

    const academicNavItems: NavItem[] = [
        {
            title: t('nav.students', 'Students'),
            href: studentsIndex(),
            icon: GraduationCap,
        },
        {
            title: t('nav.departments', 'Academic Setup'),
            href: academicIndex(),
            icon: Building2,
        },
        {
            title: t('nav.promotion', 'Student Promotion'),
            href: promotionIndex(),
            icon: BookOpen,
        },
        {
            title: t('nav.attendance', 'Attendance'),
            href: attendanceIndex(),
            icon: BookOpen,
        },
        {
            title: t('nav.exams', 'Exams & Results'),
            href: examsIndex(),
            icon: BookOpen,
        },
    ];

    const financeNavItems: NavItem[] = [
        {
            title: t('nav.fee_categories', 'Fee Structures'),
            href: feesIndex(),
            icon: Receipt,
        },
        {
            title: t('nav.invoices', 'Monthly Invoices'),
            href: invoicesIndex(),
            icon: FileText,
        },
        {
            title: t('nav.due_reports', 'Due Reports'),
            href: duesReport(),
            icon: FileText,
        },
        {
            title: t('nav.chanda_donations', 'Chanda & Donations'),
            href: chandaIndex(),
            icon: HeartHandshake,
        },
        {
            title: t('nav.expenses', 'Expenses'),
            href: expensesIndex(),
            icon: Receipt,
        },
    ];

    const systemNavItems: NavItem[] = [
        {
            title: t('nav.institutions', 'Madrasas'),
            href: institutionsIndex(),
            icon: Building2,
        },
        {
            title: t('nav.users', 'Users & Roles'),
            href: usersIndex(),
            icon: Users,
        },
        {
            title: t('nav.settings', 'Institution Settings'),
            href: settingsIndex(),
            icon: Settings,
        },
        {
            title: t('nav.profile', 'My Profile'),
            href: editProfile(),
            icon: Settings,
        },
    ];

    return (
        <Sidebar collapsible="icon" variant="sidebar">
            <SidebarHeader className="px-3 py-4">
                <SidebarMenu>
                    <SidebarMenuItem>
                        <SidebarMenuButton size="lg" asChild>
                            <Link href={dashboard()} prefetch>
                                <AppLogo />
                            </Link>
                        </SidebarMenuButton>
                    </SidebarMenuItem>
                </SidebarMenu>
            </SidebarHeader>

            <SidebarContent className="gap-2">
                <NavMain label={t('nav.overview', 'Overview')} items={overviewNavItems} />
                <NavMain label={t('nav.academic', 'Academic')} items={academicNavItems} />
                <NavMain label={t('nav.students_fees', 'Finance & Chanda')} items={financeNavItems} />
                <NavMain label={t('nav.system', 'System')} items={systemNavItems} />
            </SidebarContent>

            <SidebarFooter className="border-t border-sidebar-border p-2">
                <NavUser />
            </SidebarFooter>
        </Sidebar>
    );
}
