import { ApiError } from '$lib/api/client';
import { listMyBookmarks } from '$lib/api/bookmarks';
import { listMyCollections } from '$lib/api/collections';
import type { PageLoad } from './$types';

export const ssr = false;

/**
 * Cheap pair-fetch for the landing overview's counts. Each call asks
 * for `limit: 1` so the server still returns the paged envelope's
 * `total` without dragging the full payload across the wire.
 */
export const load: PageLoad = async () => {
    try {
        const [bookmarks, collections] = await Promise.all([
            listMyBookmarks({ limit: 1 }),
            listMyCollections({ limit: 1 })
        ]);
        return {
            authenticated: true,
            bookmark_count: bookmarks.page.total ?? 0,
            collection_count: collections.page.total ?? 0
        };
    } catch (e) {
        if (e instanceof ApiError && e.status === 401) {
            return { authenticated: false, bookmark_count: 0, collection_count: 0 };
        }
        throw e;
    }
};
