/**
 * GET /api/v1/jobs/:id — retrieve a single indexing job by ID.
 */

import { json } from '@sveltejs/kit';
import type { RequestHandler } from './$types';
import { getClient } from '$lib/server/db/client.js';
import { JobQueue } from '$lib/server/pipeline/job-queue.js';
import { handleServiceError, NotFoundError } from '$lib/server/utils/validation.js';

export const GET: RequestHandler = ({ params }) => {
	try {
		const db = getClient();
		const queue = new JobQueue(db);

		const job = queue.getJob(params.id);
		if (!job) throw new NotFoundError(`Job ${params.id} not found`);

		return json({ job });
	} catch (err) {
		return handleServiceError(err);
	}
};

export const OPTIONS: RequestHandler = () => {
	return new Response(null, {
		status: 204,
		headers: {
			'Access-Control-Allow-Origin': '*',
			'Access-Control-Allow-Methods': 'GET, OPTIONS',
			'Access-Control-Allow-Headers': 'Content-Type, Authorization'
		}
	});
};
