import { AdminEventStore } 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 eventResult = await new AdminEventStore().fetch({
		...fetchParams,
		variables: { id: event.params.event }
	});

	if (!eventResult.data?.event) {
		throw error(404, 'Event not found');
	}

	return {
		event: eventResult
	};
};
