import type { PageServerLoad } from './$types';
import { getVisibleLevels } from '$lib/server/visibility';

export const load: PageServerLoad = async ({ locals, parent }) => {
	const { member } = await parent();

	// Get visible events based on user role
	const visibleLevels = getVisibleLevels(member?.role);

	const [eventsResult, eventTypesResult] = await Promise.all([
		locals.supabase
			.from('events_with_counts')
			.select('*')
			.in('visibility', visibleLevels)
			.eq('status', 'published')
			.gte('start_datetime', new Date().toISOString())
			.order('start_datetime', { ascending: true }),
		locals.supabase
			.from('event_types')
			.select('id, name, color')
			.order('name', { ascending: true })
	]);

	return {
		events: eventsResult.data || [],
		eventTypes: eventTypesResult.data || []
	};
};

