import { useQuery } from "@tanstack/react-query";

import { connectorRepository } from "@/repositories/connectors/connector.repository";
import { queryKeys } from "@/repositories/shared/query-keys";
import { logger } from "@/utilities";

export function useConnectorDetail(connectorId: string) {
  const connectorQuery = useQuery({
    queryKey: queryKeys.connectors.detail(connectorId),
    queryFn: () => {
      logger.debug({ component: 'connectors', action: 'fetch-connector-detail', message: 'Fetching connector detail', details: { connectorId } });
      return connectorRepository.getConnector(connectorId);
    },
    enabled: !!connectorId,
  });

  const modelsQuery = useQuery({
    queryKey: queryKeys.connectors.models(connectorId),
    queryFn: () => connectorRepository.getModels(connectorId),
    enabled: !!connectorId,
  });

  return {
    connector: connectorQuery.data ?? null,
    models: modelsQuery.data ?? [],
    isLoadingConnector: connectorQuery.isLoading,
    isLoadingModels: modelsQuery.isLoading,
    isError: connectorQuery.isError || modelsQuery.isError,
    error: connectorQuery.error ?? modelsQuery.error,
  };
}
