import { AdminEventsStore } from '$houdini';
import { error } from '@sveltejs/kit';
import type { PageServerLoad } from './$types';

export const load: PageServerLoad = async (event) => {
	const { parent } = event;
	const parentData = await parent();
	const me = parentData.user?.data?.me;

	// Gate to ADMIN only
	if (me?.__typename !== 'TeamProfileType' || me.role !== 'ADMIN') {
		throw error(403, 'Admin access required');
	}

	const fetchParams = {
		event,
		metadata: { cookie: event.locals.cookie }
	};

	const events = await new AdminEventsStore().fetch({
		...fetchParams,
		variables: { limit: 100, offset: 0 }
	});

	return {
		events
	};
};
