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

export const Route = createFileRoute("/(auth)/login")({
	component: RouteComponent,
	validateSearch: z.object({
		redirect: z.string().optional(),
	}),

	beforeLoad: async () => {
		const result = await authClient.getSession({
			query: { disableCookieCache: true },
		});

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

function RouteComponent() {
	const search = Route.useSearch();
	const redirectPath = search.redirect ?? "/";

	return (
		<div className="flex justify-center mt-10">
			<LoginForm redirectPath={redirectPath} />
		</div>
	);
}
