import type { PageServerLoad } from './$types';
import { getBooksForDiscovery, getVotedBooks } from '$lib/server/pocketbase';
import type { DiscoveryPrefs } from '$lib/server/pocketbase';

export const load: PageServerLoad = async ({ locals, url }) => {
	let prefs: DiscoveryPrefs | undefined;
	const prefsParam = url.searchParams.get('prefs');
	if (prefsParam) {
		try { prefs = JSON.parse(prefsParam) as DiscoveryPrefs; } catch { /* ignore */ }
	}

	const [books, votedBooks] = await Promise.all([
		getBooksForDiscovery(locals.sessionId, locals.user?.id, prefs).catch(() => []),
		getVotedBooks(locals.sessionId, locals.user?.id).catch(() => [])
	]);
	return { books, votedBooks };
};
