import { Cat, Printer } from "lucide-react";
import type { UserRoles } from "@/lib/authClient";
import type { User } from "@/types";
import {
	SidebarGroup,
	SidebarGroupContent,
	SidebarGroupLabel,
	SidebarMenu,
	SidebarMenuButton,
	SidebarMenuItem,
} from "../../../../../../components/ui/sidebar";
import { useModuleStore } from "../../../-lib/store/useModuleStore";
import { hasPageAccess } from "../../../-utils/userAccess";

const iconMap: any = {
	Printer: Printer,
	Cat: Cat,
};

export function QualitySideBar({
	user,
	userRoles,
}: {
	user: User | null;
	userRoles: UserRoles[] | null;
}) {
	const { modules } = useModuleStore();

	const items = modules?.filter((m) => m.category === "quality" && m.active);
	const userUpdate = { ...user, roles: userRoles };

	return (
		<SidebarGroup>
			<SidebarGroupLabel>Quality</SidebarGroupLabel>
			<SidebarGroupContent>
				<SidebarMenu>
					{items.map((item) => {
						if (!item.active) return;
						const Icon = iconMap[item.icon === "" ? "Cat" : item.icon];
						return (
							<SidebarMenuItem key={item.module_id}>
								<>
									{hasPageAccess(userUpdate as any, item.roles, item.name) && (
										<SidebarMenuButton asChild>
											<a href={item.link}>
												<Icon />
												<span>{item.name}</span>
											</a>
										</SidebarMenuButton>
									)}
								</>
							</SidebarMenuItem>
						);
					})}
				</SidebarMenu>
			</SidebarGroupContent>
		</SidebarGroup>
	);
}
