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

/**
 * GET /api/scrape/task/[id]
 *
 * Returns { id, status, error_message } for a single scraping task.
 * Used by the book detail page to poll for task completion.
 */
export const GET: RequestHandler = async ({ params }) => {
	const { id } = params;
	if (!id) throw error(400, 'Missing task id');

	const task = await getScrapingTask(id).catch(() => null);
	if (!task) throw error(404, 'Task not found');

	return json({ id: task.id, status: task.status, error_message: task.error_message ?? '' });
};
