import { useQuery } from '@tanstack/react-query';
import { apiClient } from '@/api/client';
import { useAuthStore } from '@/stores/auth-store';

export function useNetworks() {
  return useQuery({
    queryKey: ['networks'],
    queryFn: () => apiClient.listNetworks(),
    meta: { toastOnError: true },
  });
}

export function useNetwork(networkId: string) {
  const { data: networks } = useNetworks();
  return networks?.find((n) => n.id === networkId) ?? null;
}

export function useIsNetworkAdmin(networkId: string): boolean {
  const network = useNetwork(networkId);
  const userId = useAuthStore((s) => s.user?.id);
  if (!network || !userId) return false;
  return network.admin_human.id === userId;
}
