import { useQuery } from 'react-query';

import { getSession } from 'src/pages/dashboard/Learning/Sessions/services';
import { type RequestError, type RequestSuccess } from 'src/types/services';
import { sToMs } from 'src/utils/timeUtils';

import { sessionsKeys } from '../queries';

type TSuccess = RequestSuccess<typeof getSession>;
type TError = RequestError;
type TData = TSuccess['data'];

export const useGetSession = (sessionId?: number) => {
  return useQuery<TSuccess, TError, TData>({
    queryKey: sessionsKeys.sessions.detail(sessionId!),
    queryFn: () => {
      if (!sessionId) throw new Error('Session ID is required');
      return getSession(sessionId);
    },
    select: response => response.data,
    staleTime: sToMs(60),
    enabled: !!sessionId,
  });
};
