import {
    Sidebar,
    SidebarContent,
    SidebarFooter,
    SidebarTrigger,
} from "../ui/sidebar";
import { ProductionSideBar } from "./side-components/production";
import { Header } from "./side-components/header";
import { LogisticsSideBar } from "./side-components/logistics";
import { QualitySideBar } from "./side-components/quality";
import { ForkliftSideBar } from "./side-components/forklift";
import { EomSideBar } from "./side-components/eom";
import { AdminSideBar } from "./side-components/admin";
import { useSessionStore } from "../../lib/store/sessionStore";
import { hasAccess } from "../../utils/userAccess";
import { moduleActive } from "../../utils/moduleActive";
import { useModuleStore } from "../../lib/store/useModuleStore";

export function AppSidebar() {
    const { user } = useSessionStore();
    const { modules } = useModuleStore();

    return (
        <Sidebar collapsible="icon">
            <SidebarContent>
                <Header />
                {moduleActive("production") && (
                    <ProductionSideBar
                        user={user}
                        moduleID={
                            modules.filter((n) => n.name === "production")[0]
                                .module_id as string
                        }
                    />
                )}
                {moduleActive("logistics") && (
                    <LogisticsSideBar
                        user={user}
                        moduleID={
                            modules.filter((n) => n.name === "logistics")[0]
                                .module_id as string
                        }
                    />
                )}
                {moduleActive("forklift") &&
                    hasAccess(user, "forklift", modules) && <ForkliftSideBar />}
                {moduleActive("eom") && hasAccess(user, "eom", modules) && (
                    <EomSideBar />
                )}
                {moduleActive("quality") &&
                    hasAccess(user, "quality", modules) && <QualitySideBar />}
                {moduleActive("admin") && hasAccess(user, "admin", modules) && (
                    <AdminSideBar />
                )}
            </SidebarContent>
            <SidebarFooter>
                <SidebarTrigger />
            </SidebarFooter>
        </Sidebar>
    );
}
