import { useQuery } from '@tanstack/react-query';
import { isAxiosError } from 'axios';
import { fetchProject } from '@/features/projects/services/projectService';

export const projectQueryKey = (id: string) => ['projects', 'detail', id] as const;

export function useProject(id: string | undefined) {
  return useQuery({
    queryKey: projectQueryKey(id ?? ''),
    queryFn: () => fetchProject(id!),
    enabled: Boolean(id),
    retry: (failureCount, error) =>
      !(isAxiosError(error) && error.response?.status === 404) && failureCount < 2,
  });
}
