import { kratosServerClient } from '$lib/kratos-server';
import { ADMIN_USER_ID } from '$env/static/private';
import type { LayoutServerLoad } from './$types';

export const load: LayoutServerLoad = async ({ cookies }) => {
	try {
		const sessionToken = cookies.get('ory_kratos_session');

		if (!sessionToken) {
			return { session: null, isAdmin: false };
		}

		const { data: session } = await kratosServerClient.toSession({
			cookie: `ory_kratos_session=${sessionToken}`
		});

		const isAdmin = session?.identity?.id === ADMIN_USER_ID;

		return { session, isAdmin };
	} catch {
		return { session: null, isAdmin: false };
	}
};
