(async () => {
    const apiClient = window.ApiClient;
    if (!apiClient) { console.error("Logged in?"); return; }

    try {
        // Fetch 1 random item ID
        const rnd = await apiClient.getItems(apiClient.getCurrentUserId(), { SortBy: "Random", Limit: 1, Recursive: true, IncludeItemTypes: "Movie,Series" });
        if (rnd.Items.length > 0) {
            const id = rnd.Items[0].Id;
            console.log("Random Item ID:", id);

            // Fetch Default Details
            const defd = await apiClient.getItem(apiClient.getCurrentUserId(), id);
            console.log("Default Fields:", defd);
            
            // Fetch ALL Known Fields manually
            const allFields = "Chapters,People,MediaStreams,UserData,RecursiveItemCount,DateCreated,MediaSources,ProductionYear,Studios,Genres,Tags,RemoteTrailers,ProviderIds,Overview,CommunityRating,CriticRating,OfficialRating,PremiereDate,RunTimeTicks";
            const full = await res.json();
            console.log("Full Details:", full);

            // Helper to download JSON
            const blob = new Blob([JSON.stringify(full, null, 2)], { type: "application/json" });
            const url = URL.createObjectURL(blob);
            const a = document.createElement("a");
            a.href = url;
            a.download = `jellyfin-item-${id}.json`;
            a.click();
            URL.revokeObjectURL(url);
            console.log("Downloaded JSON file.");
        } else { console.warn("No items."); }
    } catch (e) { console.error(e); }
})();
