import { createFileRoute, redirect } from "@tanstack/react-router";
import { authClient } from "@/lib/auth-client";

// /drinkkaart redirects to /account, preserving ?topup=success for
// backward-compatibility with the Lemon Squeezy webhook return URL.
export const Route = createFileRoute("/drinkkaart")({
	validateSearch: (search: Record<string, unknown>) => ({
		topup: search.topup as string | undefined,
	}),
	beforeLoad: async ({ search }) => {
		const session = await authClient.getSession();
		if (!session.data?.user) {
			throw redirect({ to: "/login" });
		}
		throw redirect({
			to: "/account",
			search: search.topup ? { topup: search.topup } : {},
		});
	},
	component: () => null,
});
