import { redirect } from "next/navigation";
import Dashboard from "./dashboard";
import { headers } from "next/headers";
{{#if (eq backend "self")}}
import { auth } from "@{{projectName}}/auth";
{{/if}}
import { authClient } from "@/lib/auth-client";

export default async function DashboardPage() {
	{{#if (eq backend "self")}}
	const session = await auth.api.getSession({
		headers: await headers(),
	});
	{{else}}
	const session = await authClient.getSession({
		fetchOptions: {
			headers: await headers(),
			throw: true
		}
	});
	{{/if}}

	if (!session?.user) {
		redirect("/login");
	}

	{{#if (eq payments "polar")}}
	const { data: customerState } = await authClient.customer.state({
		fetchOptions: {
			headers: await headers(),
		},
	});
	{{/if}}

	return (
		<div>
			<h1>Dashboard</h1>
			<p>Welcome {session.user.name}</p>
			<Dashboard session={session} {{#if (eq payments "polar")}}customerState={customerState}{{/if}} />
		</div>
	);
}
