import { json } from '@sveltejs/kit';
import type { RequestHandler } from './$types';

export const GET: RequestHandler = async ({ locals }) => {
	const { session, user } = await locals.safeGetSession();

	if (!session || !user) {
		return json({ error: 'Not authenticated' }, { status: 401 });
	}

	// Fetch notifications for the current user
	const { data: notifications, error } = await locals.supabase
		.from('notifications')
		.select('*')
		.eq('member_id', user.id)
		.order('created_at', { ascending: false })
		.limit(50);

	if (error) {
		console.error('Failed to fetch notifications:', error);
		return json({ error: 'Failed to fetch notifications' }, { status: 500 });
	}

	return json({ notifications: notifications || [] });
};
