import { QueryClient, QueryClientProvider } from "@tanstack/react-query";
import { StrictMode } from "react";
import { createRoot } from "react-dom/client";

import { AppShell } from "@/common/presentation/app-shell";
import { LoginPage } from "@/modules/auth/presentation/login-page";
import { useAuthStore } from "@/modules/auth/presentation/stores/auth.store";

import "./index.css";

const queryClient = new QueryClient({
	defaultOptions: {
		queries: {
			refetchOnWindowFocus: false,
			retry: 1,
		},
	},
});

function Root() {
	const isAuthenticated = useAuthStore((state) => state.isAuthenticated);
	const path = window.location.pathname;

	if (path.startsWith("/login")) {
		return <LoginPage />;
	}

	return isAuthenticated ? <AppShell /> : <LoginPage />;
}

const rootElement = document.getElementById("root");

if (!rootElement) {
	throw new Error("Root element #root not found in index.html");
}

createRoot(rootElement).render(
	<StrictMode>
		<QueryClientProvider client={queryClient}>
			<Root />
		</QueryClientProvider>
	</StrictMode>,
);
