import { render } from 'vike/abort';

import { fetchSceneRevisions } from '#/src/scenes.js';
import verifyAbility from '#/utils/verify-ability.js';

export async function onBeforeRender(pageContext) {
	if (!pageContext.user || !verifyAbility(pageContext.user, 'scene', 'update')) {
		throw render(404);
	}

	const {
		revisions,
		actors,
		tags,
		movies,
	} = await fetchSceneRevisions(null, {
		isFinalized: false,
		limit: 50,
	}, pageContext.user);

	return {
		pageContext: {
			title: pageContext.routeParams.section,
			pageProps: {
				revisions,
				actors,
				tags,
				movies,
			},
		},
	};
}
