import { redirect } from '@sveltejs/kit';
import { createApiClient } from '$lib/shared/api.server';

export async function load({
	fetch,
	locals
}: {
	fetch: typeof globalThis.fetch;
	locals: App.Locals;
}) {
	const canWrite =
		locals.user?.groups?.some((g: { permissions: string[] }) =>
			g.permissions.includes('WRITE_ALL')
		) ?? false;
	if (!canWrite) throw redirect(303, '/');

	const api = createApiClient(fetch);
	const result = await api.GET('/api/documents/incomplete');

	const documents = result.response.ok ? (result.data ?? []) : [];

	return { documents, canWrite };
}
