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

/**
 * GET /api/admin/scrape-tasks
 * Returns the current scraping task list (served from 30 s Valkey cache).
 * Used by the admin scrape page for lightweight polling instead of invalidateAll().
 */
export const GET: RequestHandler = async ({ locals }) => {
	if (!locals.user || locals.user.role !== 'admin') {
		throw error(403, 'Forbidden');
	}
	const tasks = await listScrapingTasks().catch(() => []);
	return json({ tasks });
};
