import { getManga, getSimilarMangas, type MangaCard } from '$lib/api/mangas';
import { listChapters } from '$lib/api/chapters';
import { listMyBookmarksOrEmpty } from '$lib/api/bookmarks';
import { getMyReadProgressForManga } from '$lib/api/read_progress';
import type { PageLoad } from './$types';

export const ssr = false;

export const load: PageLoad = async ({ params }) => {
    const [manga, chapters, bookmarks, readProgress, similar] = await Promise.all([
        getManga(params.id),
        listChapters(params.id),
        listMyBookmarksOrEmpty(),
        // Null when guest or never-read — page handles both cases.
        getMyReadProgressForManga(params.id),
        // Recommendations are non-critical: a failure here must not break
        // the detail page, so fall back to an empty list.
        getSimilarMangas(params.id).catch(() => [] as MangaCard[])
    ]);
    return {
        manga,
        chapters: chapters.items,
        bookmarks: bookmarks.items,
        readProgress,
        similar
    };
};
