import { redirect } from '@sveltejs/kit';
import type { PageServerLoad } from './$types';
import { getSubscriptionFeed } from '$lib/server/pocketbase';
import { log } from '$lib/server/logger';
import type { Book } from '$lib/server/pocketbase';

export const load: PageServerLoad = async ({ locals }) => {
	if (!locals.user) {
		redirect(302, '/login?next=/feed');
	}

	const feed = await getSubscriptionFeed(locals.user.id, 50).catch((e) => {
		log.error('feed', 'failed to load subscription feed', { err: String(e) });
		return [] as Array<{ book: Book; readerUsername: string }>;
	});

	return { feed };
};
