import { AdminNotificationRulesStore, AdminProfilesStore } 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 }
	};

	// Fetch notification rules and profiles in parallel
	const [rules, profiles] = await Promise.all([
		new AdminNotificationRulesStore().fetch(fetchParams),
		new AdminProfilesStore().fetch(fetchParams)
	]);

	return {
		rules,
		profiles
	};
};
