import { createFileRoute, redirect } from "@tanstack/react-router";
import { z } from "zod";
import { authClient } from "../../../lib/authClient";
import LoginForm from "./-components/LoginForm";

export const Route = createFileRoute("/_app/(auth)/login")({
	component: RouteComponent,
	validateSearch: z.object({
		redirect: z.string().optional(),
	}),
	beforeLoad: async () => {
		const result = await authClient.getSession({
			query: { disableCookieCache: true }, // force DB/Server lookup
		});

		//console.log("session check:", result.data);

		if (result.data) {
			throw redirect({ to: "/" });
		}
	},
});

function RouteComponent() {
	const search = Route.useSearch();
	const redirectPath = search.redirect ?? "/";
	return (
		<div className="ml-[25%] mt-[0.5%]">
			<LoginForm redirectPath={redirectPath} />
		</div>
	);
}
