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

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

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

	// Mark all unread notifications as read for the current user
	const { error } = await locals.supabase
		.from('notifications')
		.update({ read_at: new Date().toISOString() })
		.eq('member_id', user.id)
		.is('read_at', null);

	if (error) {
		console.error('Failed to mark all notifications as read:', error);
		return json({ error: 'Failed to update notifications' }, { status: 500 });
	}

	return json({ success: true });
};
