/**
 * POST /api/admin/ai-jobs/[id]/cancel
 *
 * Admin-only proxy to the Go backend's AI job cancel endpoint.
 */

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

export const POST: RequestHandler = async ({ params, locals }) => {
	if (!locals.user || locals.user.role !== 'admin') {
		throw error(403, 'Forbidden');
	}

	const { id } = params;

	let res: Response;
	try {
		res = await backendFetch(`/api/admin/ai-jobs/${id}/cancel`, { method: 'POST' });
	} catch (e) {
		log.error('admin/ai-jobs/cancel', 'backend proxy error', { id, err: String(e) });
		throw error(502, 'Could not reach backend');
	}

	const data = await res.json().catch(() => ({}));
	return json(data, { status: res.status });
};
