import { useRouter } from "@tanstack/react-router";
import { useEffect } from "react";
import { useModuleStore } from "../../routes/_old/old/-lib/store/useModuleStore";
import { useSettingStore } from "../../routes/_old/old/-lib/store/useSettings";
import { useSession, useUserRoles } from "../authClient";

export function SessionGuard({ children }: { children: React.ReactNode }) {
	const { data: session, isLoading } = useSession();
	const { fetchRoles } = useUserRoles();
	const { fetchModules } = useModuleStore();
	const { fetchSettings } = useSettingStore();
	const router = useRouter();

	useEffect(() => {
		// if (!isLoading && !session) {
		//     router.navigate({ to: "/" }); // redirect if not logged in
		// }
		fetchModules();
		fetchSettings();
		//if (session) {
		fetchRoles();
		//}
	}, [isLoading, session, router]);

	if (isLoading) return <div>App Loading</div>;
	return <>{children}</>;
}
