(async () => {
    const apiClient = window.ApiClient;
    if (!apiClient) {
        console.error("ApiClient not found. Are you logged in?");
        return;
    }

    const userId = apiClient.getCurrentUserId();
    const serverAddress = apiClient.serverAddress();
    
    // Example test configuration flags
    const maxItems = 50;
    const includeWatchedContent = false; // set to false to ONLY show unplayed (newly watched)
    const maxParentalRating = 12; // Test age limit
    const maxDaysRecent = 30;     // Test recency limit

    // Build the query parameters just like in mediaBarEnhanced.js
    const sortParams = "sortBy=Random";
    const playedFilter = includeWatchedContent ? '' : '&isPlayed=False';
    
    let parentalFilter = '';
    if (maxParentalRating) {
      parentalFilter = `&MaxOfficialRating=${maxParentalRating}`;
    }

    let dateFilter = '';
    if (maxDaysRecent) {
      const pastDate = new Date();
      pastDate.setDate(pastDate.getDate() - maxDaysRecent);
      dateFilter = `&MinDateCreated=${pastDate.toISOString()}`;
    }

    const apiUrl = `${serverAddress}/Items?IncludeItemTypes=Movie,Series&Recursive=true&hasOverview=true&imageTypes=Logo,Backdrop&${sortParams}${playedFilter}${parentalFilter}${dateFilter}&enableUserData=true&Limit=${maxItems}&fields=Id&userId=${userId}`;

    try {
        console.log(`Testing generated URL with filters:\n%c${apiUrl}`, "color: yellow;");

        // Execute the fetch
        const response = await fetch(apiUrl, {
            headers: {
                Authorization: `MediaBrowser Client="${apiClient.appName()}", Device="${apiClient.deviceName()}", DeviceId="${apiClient.deviceId()}", Version="${apiClient.appVersion()}", Token="${apiClient.accessToken()}"`
            }
        });

        if (!response.ok) {
             throw new Error(`Failed to fetch items: ${response.status} ${response.statusText}`);
        }

        const data = await response.json();
        const items = data.Items || [];

        console.log(`%cErgebnis: ${items.length} Items gefunden!`, "color: #00a4dc; font-weight: bold;");
        
        if(items.length > 0) {
            console.log("Erstes Item als Beispiel:");
            console.dir(items[0]);
        }

    } catch (error) {
        console.error("Fehler beim Abrufen der URL:", error);
    }
})();
