import type { PageServerLoad } from './$types';
import { User } from '$lib/server/database';
import { getSession } from '$lib/server/session';
import { Permissions } from '$lib/permissions';
import { redirect } from '@sveltejs/kit';
import { env } from '$env/dynamic/public';

export const load: PageServerLoad = async ({ parent, cookies }) => {
	const { userCount } = await parent();
	if (userCount == null) throw redirect(302, `${env.PUBLIC_BASE_PATH}/admin`);

	return {
		count:
			getSession(cookies, { permissions: [Permissions.UserRead] }) != null ? await User.count() : 0
	};
};
