import { useQuery } from '@tanstack/react-query';
import { config } from '../../config';

interface HealthResponse {
  status: string;
  components?: {
    serverCapabilities?: {
      details?: {
        infrastructureEndpoints?: boolean;
      };
    };
  };
}

export function useServerCapabilities() {
  return useQuery<{ infrastructureEndpoints: boolean }>({
    queryKey: ['server-capabilities'],
    queryFn: async () => {
      const res = await fetch(config.apiBaseUrl + '/health');
      if (!res.ok) return { infrastructureEndpoints: true };
      const data: HealthResponse = await res.json();
      return {
        infrastructureEndpoints:
          data.components?.serverCapabilities?.details?.infrastructureEndpoints ?? true,
      };
    },
    staleTime: Infinity,
  });
}
