import { infiniteQueryOptions, mutationOptions, queryOptions } from "@tanstack/react-query";
import { rommApi, storeApi } from "../clientApi";
import { GameListFilterType, FrontEndGameType } from '@simeonradivoev/gameflow-sdk/shared';

export const storeEmulatorsQuery = (filters: { search?: string; }) => queryOptions({
    queryKey: ['store-emulators', filters], queryFn: async () =>
    {
        const { data, error } = await storeApi.api.store.emulators.get({ query: { search: filters.search } });
        if (error) throw new Error(JSON.stringify(error.value));
        return data;
    }
});
export const storeFeaturedGamesQuery = queryOptions({
    queryKey: ['store-emulators', 'featured'], queryFn: async () =>
    {
        const { data, error } = await storeApi.api.store.games.featured.get();
        if (error) throw error;
        return data;
    }
});
export const storeEmulatorsRecommendedQuery = (id?: string) => queryOptions({
    queryKey: ['store-emulators', 'recommended', id ?? 'all'], queryFn: async () =>
    {
        const { data, error } = await storeApi.api.store.emulators.get({ query: { limit: 6, missing: true, orderBy: 'importance', related: id } });
        if (error) throw error;
        return data;
    }
});
export const storeEmulatorDetailsQuery = (id: string) => queryOptions({
    queryKey: ['store-emulator', id], queryFn: async () =>
    {
        const { data, error } = await storeApi.api.store.emulator({ id }).get();
        if (error) throw error;
        return data;
    }
});
export const storeEmulatorDeleteMutation = mutationOptions({
    mutationKey: ['store-emulator', 'delete'],
    mutationFn: async (id: string) =>
    {
        const { error } = await storeApi.api.store.emulator({ id }).delete();
        if (error) throw error;
    }
});

export const storeGamesInfiniteQuery = (filter: GameListFilterType) => infiniteQueryOptions<{ data: FrontEndGameType[], nextPage: number; }>({
    initialPageParam: 0,
    queryKey: ['store-games', filter],
    getNextPageParam: (lastPage, pages) => lastPage.nextPage,
    queryFn: async (data) =>
    {
        const pageParam = data.pageParam as number;
        const { data: games, error } = await rommApi.api.romm.games.get({ query: { ...filter, source: 'store', offset: pageParam * 10, limit: 10 } });
        if (error) throw error;
        return { data: games.games, nextPage: pageParam + 1 };
    }
});

export const storeGetStatsQuery = queryOptions({
    queryKey: ['store', 'stats'], queryFn: async () =>
    {
        const { data, error } = await storeApi.api.store.stats.get();
        if (error) throw error;
        return data;
    }
});
export const installEmulatorMutation = (id: string) => mutationOptions({
    mutationKey: ['install', 'emulator', id],
    mutationFn: async (ctx: { source: string, isUpdate: boolean; }) =>
    {
        const { data, error } = await storeApi.api.store.install.emulator({ id })({ source: ctx.source }).post({ isUpdate: ctx.isUpdate });
        if (error) throw error;
        return data;
    }
});
export const downloadBiosMutation = (id: string) => mutationOptions({
    mutationKey: ["download", 'bios', id],
    mutationFn: async () =>
    {
        const { error } = await storeApi.api.store.download.bios({ id }).post();
        if (error) throw error;
    }
});
export const deleteBiosMutation = mutationOptions({
    mutationKey: ["delete", "bios"], mutationFn: async (id: string) =>
    {
        const { error } = await storeApi.api.store.bios({ id }).delete();
        if (error) throw error;
    }
});
export const getUpdateInfoForEmulator = (id: string) => queryOptions({
    queryKey: ['emulator', 'update'], queryFn: async () =>
    {
        const { data, error } = await storeApi.api.store.emulator({ id }).update.get();
        if (error) throw error;
        return data;
    }
});
export const pluginsQuery = (search?: string) => queryOptions({
    queryKey: ['plugins', 'store', search ?? 'all'],
    queryFn: async () =>
    {
        const { data, error } = await storeApi.api.store.plugins.get({ query: { search } });
        if (error) throw error;
        return data;
    }
});
export const pluginDetailsQuery = (id: string) => queryOptions({
    queryKey: ['plugin', 'store', id],
    queryFn: async () =>
    {
        const { data, error } = await storeApi.api.store.plugin.get({ query: { plugin: id } });
        if (error) throw error;
        return data;
    }
});