import { LstCard } from "@/components/extendedUI/LstCard";
import {
    Table,
    TableBody,
    TableCell,
    TableHead,
    TableHeader,
    TableRow,
} from "@/components/ui/table";
import { useSessionStore } from "@/lib/store/sessionStore";
import { useModuleStore } from "@/lib/store/useModuleStore";
import { useQuery } from "@tanstack/react-query";
import { useRouter } from "@tanstack/react-router";
import { ChangeSubModule } from "./SubModuleForm";

import { Skeleton } from "@/components/ui/skeleton";
import { getSubModules } from "@/utils/querys/admin/subModules";
import { useEffect } from "react";

export type Settings = {
    settings_id?: string;
    name?: string;
    value?: string;
    description?: string;
};

export default function SubModulePage() {
    const { user, token } = useSessionStore();
    const { modules } = useModuleStore();
    const router = useRouter();

    // const adminModule = modules.filter((n) => n.name === "admin");
    // const userLevel =
    //     user?.roles.filter((r) => r.module_id === adminModule[0].module_id) ||
    //     [];

    // if (!adminModule[0]?.roles.includes(userLevel[0]?.role)) {
    //     //router.navigate({ to: "/" });
    // }

    useEffect(() => {
        if (!user || modules.length === 0) return;

        const adminModule = modules.find((n) => n.name === "admin");
        if (!adminModule) {
            console.log("no module loaded");
            //router.navigate({ to: "/" });
            return;
        }

        const userLevel =
            user?.roles?.filter((r) => r.module_id === adminModule.module_id) ||
            [];

        if (!adminModule.roles?.includes(userLevel[0]?.role)) {
            console.log("Something failed");
            //router.navigate({ to: "/" });
        }
    }, [modules, user, router]);

    const { data, isError, error, isLoading } = useQuery(
        getSubModules(token ?? "")
    );

    // if (isLoading) {
    //     return <div>Loading.....</div>;
    // }
    if (isError) {
        return <div>{JSON.stringify(error)}</div>;
    }

    return (
        <LstCard className="m-2 flex place-content-center w-fit">
            <Table>
                <TableHeader>
                    <TableRow>
                        <TableHead>Name</TableHead>
                        <TableHead>Description</TableHead>
                        <TableHead>Module In</TableHead>
                        <TableHead>Roles</TableHead>
                        <TableHead>Active</TableHead>
                        <TableHead>Edit</TableHead>
                    </TableRow>
                </TableHeader>
                {isLoading ? (
                    <>
                        <TableBody>
                            {Array(10)
                                .fill(0)
                                .map((_, i) => (
                                    <TableRow key={i}>
                                        <TableCell className="font-medium">
                                            <Skeleton className="h-4" />
                                        </TableCell>
                                        <TableCell>
                                            <Skeleton className="h-4" />
                                        </TableCell>
                                        <TableCell>
                                            <Skeleton className="h-4" />
                                        </TableCell>
                                        <TableCell>
                                            <Skeleton className="h-4" />
                                        </TableCell>
                                        <TableCell>
                                            <Skeleton className="h-4" />
                                        </TableCell>
                                        <TableCell>
                                            <Skeleton className="h-4" />
                                        </TableCell>
                                        <TableCell>
                                            <Skeleton className="h-4" />
                                        </TableCell>
                                    </TableRow>
                                ))}
                        </TableBody>
                    </>
                ) : (
                    <TableBody>
                        {data?.map((i: any) => (
                            <TableRow key={i.submodule_id}>
                                <TableCell className="font-medium">
                                    {i.name}
                                </TableCell>
                                <TableCell className="font-medium">
                                    {i.description}
                                </TableCell>
                                <TableCell className="font-medium">
                                    {i.moduleName}
                                </TableCell>
                                <TableCell className="font-medium">
                                    {JSON.stringify(i.roles)}
                                </TableCell>
                                <TableCell className="font-medium">
                                    {i.active ? "Yes" : "No"}
                                </TableCell>
                                <TableCell className="font-medium">
                                    <ChangeSubModule subModule={i} />
                                </TableCell>
                            </TableRow>
                        ))}
                    </TableBody>
                )}
            </Table>
        </LstCard>
    );
}
