import { json, error } from '@sveltejs/kit';
import type { RequestHandler } from './$types';
import { backendFetch } from '$lib/server/scraper';

export const GET: RequestHandler = async ({ url }) => {
	const userId = url.searchParams.get('user_id');
	if (!userId) throw error(400, 'user_id required');
	const res = await backendFetch('/api/notifications?user_id=' + userId);
	const data = await res.json().catch(() => ({ notifications: [] }));
	return json(data);
};

// PATCH /api/notifications?user_id=<id> — mark all read
export const PATCH: RequestHandler = async ({ url }) => {
	const userId = url.searchParams.get('user_id');
	if (!userId) throw error(400, 'user_id required');
	const res = await backendFetch('/api/notifications?user_id=' + userId, { method: 'PATCH' });
	const data = await res.json().catch(() => ({}));
	return json(data);
};

// DELETE /api/notifications?user_id=<id> — clear all
export const DELETE: RequestHandler = async ({ url }) => {
	const userId = url.searchParams.get('user_id');
	if (!userId) throw error(400, 'user_id required');
	const res = await backendFetch('/api/notifications?user_id=' + userId, { method: 'DELETE' });
	const data = await res.json().catch(() => ({}));
	return json(data);
};
