import listQuerier from "~/lib/server/dbHelpers/listQuerier";
import { Band } from "~/models/band";
import { Story } from "~/models/stories";

export default eventHandler(async (event) => {
	const params = getRouterParams(event);
	const query = getQuery(event);
	let band = await Band.findById(params.id);
	if (!band) {
		throw createError({ statusCode: 404, message: "not found." });
	}
	let skipAmt = 20 * (parseInt((query.page as string) || "1") - 1) - 1;
	if (skipAmt < 0) skipAmt = 0;
	let stories = await listQuerier(
		{
			"chapters.bands": {
				$in: [parseInt(params["id"])],
			},
		},
		event.context,
	); /*  */
	return {
		...band.toObject(),
		stories: stories /* .slice(skipAmt, skipAmt + 20 + 1) */,
		total: stories.length,
	};
});
