import type { PageServerLoad } from './$types';

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

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

	const { data: events } = await locals.supabase
		.from('events_with_counts')
		.select('*')
		.in('visibility', visibleLevels)
		.eq('status', 'published')
		.gte('start_datetime', new Date().toISOString())
		.order('start_datetime', { ascending: true });

	return {
		events: events || []
	};
};

function getVisibleLevels(role: string | undefined): string[] {
	switch (role) {
		case 'admin':
			return ['public', 'members', 'board', 'admin'];
		case 'board':
			return ['public', 'members', 'board'];
		default:
			return ['public', 'members'];
	}
}
