(async () => {
    // 1. Initialisierung
    const apiClient = window.ApiClient;
    if (!apiClient) {
        console.error("❌ ApiClient nicht gefunden. Bitte in der Browser-Konsole einer Jellyfin-Seite ausführen.");
        return;
    }

    // 2. Item ID abfragen oder festlegen
    let itemId = prompt("Bitte die Item ID eingeben (von einem Film/Serie mit Theme Video/Backdrop Video):");
    if (!itemId) {
        console.warn("Keine Item ID eingegeben. Abbruch.");
        return;
    }
    itemId = itemId.trim();

    const userId = apiClient.getCurrentUserId();
    console.log(`%c🔍 Untersuche Item: ${itemId}`, "color: #00a4dc; font-size: 1.2em; font-weight: bold;");

    // 3. Helper Funktion für Fetch requests
    const fetchJson = async (url) => {
        try {
            const res = await fetch(url, {
                headers: {
                    'Authorization': `MediaBrowser Client="Jellyfin Web", Device="Browser", DeviceId="${apiClient.deviceId()}", Version="${apiClient.appVersion()}", Token="${apiClient.accessToken()}"`
                }
            });
            if (res.ok) return await res.json();
            console.warn(`⚠️ Request fehlgeschlagen: ${url} (${res.status})`);
            return null;
        } catch (e) {
            console.error(`❌ Fehler bei Request: ${url}`, e);
            return null;
        }
    };

    // ---------------------------------------------------------
    // TEST 1: Standard Item Details mit erweiterten Feldern
    // ---------------------------------------------------------
    console.group("1. Standard Item Details (mit Fields)");
    const fields = "Overview,RemoteTrailers,MediaSources,LocalTrailerCount,ThemeVideoIds,ThemeSongIds";
    const itemDetailsUrl = `${apiClient.serverAddress()}/Users/${userId}/Items/${itemId}?Fields=${fields}`;
    
    const item = await fetchJson(itemDetailsUrl);
    if (item) {
        console.log("Name:", item.Name);
        console.log("ThemeVideoIds:", item.ThemeVideoIds);
        console.log("ThemeSongIds:", item.ThemeSongIds);
        console.log("LocalTrailerCount:", item.LocalTrailerCount);
        console.log("RemoteTrailers:", item.RemoteTrailers);
        
        if (item.ThemeVideoIds && item.ThemeVideoIds.length > 0) {
            console.log(`%c✅ ThemeVideoIds gefunden: ${item.ThemeVideoIds.length}`, "color: green; font-weight: bold;");
        } else {
             console.log(`%c❌ Keine ThemeVideoIds im Item-Objekt.`, "color: orange;");
        }
    }
    console.groupEnd();

    // ---------------------------------------------------------
    // TEST 2: ThemeMedia Endpoint
    // ---------------------------------------------------------
    console.group("2. Endpoint: /Items/{Id}/ThemeMedia");
    const themeMediaUrl = `${apiClient.serverAddress()}/Items/${itemId}/ThemeMedia?userId=${userId}`;
    const themeMedia = await fetchJson(themeMediaUrl);
    
    if (themeMedia) {
        console.dir(themeMedia);
        if (themeMedia.ThemeVideos && themeMedia.ThemeVideos.length > 0) {
            console.log(`%c✅ ThemeVideos gefunden: ${themeMedia.ThemeVideos.length}`, "color: green; font-weight: bold;");
            themeMedia.ThemeVideos.forEach(v => console.log(`   - ID: ${v.Id}, Name: ${v.Name}, Path: ${v.Path}`));
        } else {
            console.log("❌ Keine ThemeVideos in ThemeMedia gefunden.");
        }
    }
    console.groupEnd();

    // ---------------------------------------------------------
    // TEST 3: ThemeVideos Endpoint (Spezifisch)
    // ---------------------------------------------------------
    console.group("3. Endpoint: /Items/{Id}/ThemeVideos");
    // Manchmal auch unter Users/{UserId}/Items/{Id}/ThemeVideos
    const themeVideosUrl = `${apiClient.serverAddress()}/Items/${itemId}/ThemeVideos?userId=${userId}`;
    const themeVideos = await fetchJson(themeVideosUrl);
    
    if (themeVideos) {
        // Kann Array oder Objekt mit Items sein
        const videos = Array.isArray(themeVideos) ? themeVideos : (themeVideos.Items || []);
        console.dir(videos);
        
        if (videos.length > 0) {
             console.log(`%c✅ ThemeVideos Endpoint lieferte Ergebnisse: ${videos.length}`, "color: green; font-weight: bold;");
        } else {
             console.log("❌ ThemeVideos Endpoint lieferte leeres Array.");
        }
    }
    console.groupEnd();

    // ---------------------------------------------------------
    // TEST 4: LocalTrailers Endpoint
    // ---------------------------------------------------------
    console.group("4. Endpoint: /Items/{Id}/LocalTrailers");
    const localTrailersUrl = `${apiClient.serverAddress()}/Users/${userId}/Items/${itemId}/LocalTrailers`;
    const localTrailers = await fetchJson(localTrailersUrl);

    if (localTrailers && localTrailers.length > 0) {
        console.log(`%cℹ️ LocalTrailers gefunden: ${localTrailers.length}`, "color: blue;");
        console.dir(localTrailers);
    } else {
        console.log("❌ Keine LocalTrailers gefunden.");
    }
    console.groupEnd();

    console.log("%cFertig.", "font-weight: bold;");

})();
