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

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

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

	const notificationId = params.id;

	if (!notificationId) {
		return json({ error: 'Notification ID required' }, { status: 400 });
	}

	// Mark notification as read (RLS ensures user can only update their own)
	const { error } = await locals.supabase
		.from('notifications')
		.update({ read_at: new Date().toISOString() })
		.eq('id', notificationId)
		.eq('member_id', user.id);

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

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