import { rankingQueryKeys } from '../ranking-query-keys';
import {
  rankingApi,
  type PostGetUNTypes,
  type PostGetUNType,
  type YearUNType,
  type CountryUNType
} from '../ranking-api';
import { queryClient } from 'src/utils/queryClient';

export const fetchUNMastersTypes = async () => {
  try {
    const data: PostGetUNTypes = await queryClient.fetchQuery({
      queryKey: rankingQueryKeys.getUNMastersTypes(),
      queryFn: async () => {
        const response = await rankingApi.getUNMastersTypes();
        return response.data;
      },
      gcTime: 0,
      staleTime: 0
    });

    return data;
  } catch (error) {
    console.error('Failed to fetch UN masters types:', error);
  }
};

export const fetchUNMastersType = async (type: number) => {
  try {
    const data: PostGetUNType | CountryUNType | YearUNType = await queryClient.fetchQuery({
      queryKey: rankingQueryKeys.getUNMastersType(type),
      queryFn: async () => {
        const response = await rankingApi.getUNMastersType(type);
        return response.data;
      },
      gcTime: 0,
      staleTime: 0
    });

    return data;
  } catch (error) {
    console.error('Failed to fetch UN masters type:', error);
  }
};
