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

export const GET: RequestHandler = async ({ locals }) => {
	if (!locals.user || locals.user.role !== 'admin') {
		throw error(403, 'Forbidden');
	}
	let res: Response;
	try {
		res = await backendFetch('/api/admin/text-gen/models');
	} catch {
		throw error(502, 'Could not reach backend');
	}
	const data = await res.json().catch(() => ({}));
	return json(data, { status: res.status });
};
