import { env } from "@/env.mjs";
import { getFavoriteGames, getGames } from '@/lib/igdb';

describe('Integration Tests', () => {
    // Vor jedem Test den IGDB_BASE_URL-Wert speichern und Mock für fetch setzen
    beforeEach(() => {
        process.env.IGDB_BASE_URL = env.IGDB_BASE_URL;
        global.fetch = jest.fn();
    });

    // Nach jedem Test den IGDB_BASE_URL-Wert wiederherstellen
    afterEach(() => {
        process.env.IGDB_BASE_URL = undefined;
        jest.resetAllMocks();
    });

    test('getGames should fetch games from IGDB API', async () => {
        const mockGames = [{ id: 1, name: 'Game 1' }, { id: 2, name: 'Game 2' }];
        const mockResponse = { ok: true, json: jest.fn().mockResolvedValue(mockGames) };
        (fetch as jest.Mock).mockResolvedValue(mockResponse);

        const games = await getGames();

        expect(fetch).toHaveBeenCalledWith(
            `${env.IGDB_BASE_URL}/games`,
            expect.objectContaining({
                method: 'POST',
                headers: expect.objectContaining({
                    'Client-ID': expect.any(String),
                    Authorization: expect.stringMatching(/^Bearer /),
                }),
                body: expect.stringContaining('fields name, cover.image_id;'),
            })
        );

        expect(games).toEqual(mockGames);
    });

    test('getFavoriteGames should fetch favorite games from IGDB API', async () => {
        const mockGameList = [1, 2, 3];
        const mockGames = [{ id: 1, name: 'Game 1' }, { id: 2, name: 'Game 2' }];
        const mockResponse = { ok: true, json: jest.fn().mockResolvedValue(mockGames) };
        (fetch as jest.Mock).mockResolvedValue(mockResponse);

        const games = await getFavoriteGames(mockGameList);

        expect(fetch).toHaveBeenCalledWith(
            `${env.IGDB_BASE_URL}/games`,
            expect.objectContaining({
                method: 'POST',
                headers: expect.objectContaining({
                    'Client-ID': expect.any(String),
                    Authorization: expect.stringMatching(/^Bearer /),
                }),
                body: expect.stringContaining(`where id = (${mockGameList.toString()})`),
            })
        );

        expect(games).toEqual(mockGames);
    });
});