import { error } from '@sveltejs/kit';
import type { PageServerLoad } from './$types';
import { getBook, listChapterIdx, getProgress } from '$lib/server/pocketbase';
import { log } from '$lib/server/logger';

export const load: PageServerLoad = async ({ params, locals }) => {
	const { slug } = params;

	const book = await getBook(slug).catch((e) => {
		log.error('chapters', 'getBook failed', { slug, err: String(e) });
		return null;
	});

	if (!book) error(404, `Book "${slug}" not found`);

	let chapters, progress;
	try {
		[chapters, progress] = await Promise.all([
			listChapterIdx(slug),
			getProgress(locals.sessionId, slug, locals.user?.id)
		]);
	} catch (e) {
		log.error('chapters', 'failed to load chapters', { slug, err: String(e) });
		throw error(500, 'Failed to load chapters');
	}

	return {
		book: { slug: book.slug, title: book.title, cover: book.cover ?? '', totalChapters: book.total_chapters },
		chapters,
		lastChapter: progress?.chapter ?? null
	};
};
