import {
    ChartColumn,
    CalendarDays,
    LayoutGrid,
    LayoutTemplate,
    Mail,
    MapPin,
    Package,
    Plug,
    Sparkles,
    Tag,
    Truck,
    UserCircle,
    Users,
    Wrench,
} from 'lucide-react';
import { dashboard } from '@/routes';
import { index as adminAddons } from '@/routes/admin/addons';
import { edit as adminHomepageLayout } from '@/routes/admin/homepage-layout';
import { index as adminHomepageFaqs } from '@/routes/admin/homepage-faqs';
import { index as adminBookings } from '@/routes/admin/bookings';
import { index as adminCustomers } from '@/routes/admin/customers';
import { index as adminFleet } from '@/routes/admin/fleet';
import { index as adminLocations } from '@/routes/admin/locations';
import { index as adminPackages } from '@/routes/admin/packages';
import { index as adminEmailTemplates } from '@/routes/admin/email-templates';
import { index as adminSentEmails } from '@/routes/admin/sent-emails';
import { index as adminDiscounts } from '@/routes/admin/discounts';
import { index as adminPlugins } from '@/routes/admin/plugins';
import { index as adminReports } from '@/routes/admin/reports';
import { index as adminUsers } from '@/routes/admin/users';
import { index as adminVehicleSizes } from '@/routes/admin/vehicle-sizes';
import { show as bookLocation } from '@/routes/booking/location';
import { show as customerPortal } from '@/routes/customer/portal';
import { index as technicianJobs, live as technicianJobsLive } from '@/routes/technician/jobs';
import type { NavGroup } from '@/types';
import type { UserRole } from '@/types/auth';

function hasRole(role: UserRole | undefined, allowed: UserRole[]): boolean {
    return role !== undefined && allowed.includes(role);
}

export function getSidebarNavGroups(role?: UserRole): NavGroup[] {
    const groups: NavGroup[] = [
        {
            label: 'General',
            items: [
                {
                    title: 'Dashboard',
                    href: dashboard(),
                    icon: LayoutGrid,
                },
            ],
        },
    ];

    if (hasRole(role, ['admin', 'manager', 'technician'])) {
        const bookingItems = [];

        if (hasRole(role, ['admin', 'manager'])) {
            bookingItems.push(
                {
                    title: 'Bookings',
                    href: adminBookings(),
                    icon: CalendarDays,
                    matchPrefix: true,
                },
                {
                    title: 'Reports',
                    href: adminReports(),
                    icon: ChartColumn,
                    matchPrefix: true,
                },
            );
        }

        if (hasRole(role, ['admin', 'technician'])) {
            bookingItems.push({
                title: 'Technician jobs',
                href: technicianJobs(),
                icon: Wrench,
                matchPrefix: true,
                children: [
                    { title: 'All jobs', href: technicianJobs(), matchPrefix: false },
                    { title: 'Live', href: technicianJobsLive(), matchPrefix: true },
                ],
            });
        }

        groups.push({ label: 'Bookings', items: bookingItems });
    }

    if (hasRole(role, ['admin', 'manager'])) {
        groups.push({
            label: 'Management',
            items: [
                {
                    title: 'User Management',
                    href: hasRole(role, ['admin'])
                        ? adminUsers()
                        : adminCustomers(),
                    icon: Users,
                    matchPrefix: true,
                    children: [
                        ...(hasRole(role, ['admin'])
                            ? [
                                  {
                                      title: 'Users',
                                      href: adminUsers(),
                                      matchPrefix: true,
                                  },
                              ]
                            : []),
                        {
                            title: 'Customers',
                            href: adminCustomers(),
                            matchPrefix: true,
                        },
                    ],
                },
                {
                    title: 'Locations',
                    href: adminLocations(),
                    icon: MapPin,
                    matchPrefix: true,
                },
                {
                    title: 'Packages',
                    href: adminPackages(),
                    icon: Package,
                    matchPrefix: true,
                    children: [
                        {
                            title: 'All packages',
                            href: adminPackages(),
                            matchPrefix: true,
                        },
                        {
                            title: 'Add-ons',
                            href: adminAddons(),
                            matchPrefix: true,
                        },
                        {
                            title: 'Vehicle sizes',
                            href: adminVehicleSizes(),
                            matchPrefix: true,
                        },
                    ],
                },
                {
                    title: 'Fleet',
                    href: adminFleet(),
                    icon: Truck,
                    matchPrefix: true,
                },
                {
                    title: 'Discounts',
                    href: adminDiscounts(),
                    icon: Tag,
                    matchPrefix: true,
                },
                {
                    title: 'Email',
                    href: adminEmailTemplates(),
                    icon: Mail,
                    matchPrefix: true,
                    children: [
                        { title: 'Templates', href: adminEmailTemplates(), matchPrefix: true },
                        { title: 'Sent Log', href: adminSentEmails(), matchPrefix: true },
                    ],
                },
                {
                    title: 'Plugins',
                    href: adminPlugins(),
                    icon: Plug,
                    matchPrefix: true,
                },
            ],
        });

        groups.push({
            label: 'Website Settings',
            items: [
                {
                    title: 'Homepage layout',
                    href: adminHomepageLayout(),
                    icon: LayoutTemplate,
                    matchPrefix: true,
                    children: [
                        {
                            title: 'Layout',
                            href: adminHomepageLayout(),
                            matchPrefix: true,
                        },
                        {
                            title: 'FAQs',
                            href: adminHomepageFaqs(),
                            matchPrefix: true,
                        },
                    ],
                },
            ],
        });
    }

    if (hasRole(role, ['customer'])) {
        groups.push({
            label: 'Customer',
            items: [
                {
                    title: 'My portal',
                    href: customerPortal(),
                    icon: UserCircle,
                    matchPrefix: true,
                },
                {
                    title: 'Book a wash',
                    href: bookLocation(),
                    icon: Sparkles,
                    matchPrefix: true,
                },
            ],
        });
    }

    return groups.filter((group) => group.items.length > 0);
}
