import { trpc } from '@/lib/trpc'

export function useTermineListe(upcoming = true) {
  const { data, isLoading, refetch, isFetching } = trpc.termine.list.useQuery({ upcoming })

  return {
    data: data ?? [],
    isLoading,
    refetch,
    isRefetching: isFetching,
  }
}

export function useTerminDetail(id: string) {
  const { data, isLoading } = trpc.termine.byId.useQuery({ id })
  return { data: data ?? null, isLoading }
}

export function useToggleAnmeldung() {
  const utils = trpc.useUtils()
  const mutation = trpc.termine.toggleAnmeldung.useMutation({
    onSuccess: () => {
      utils.termine.list.invalidate()
      utils.termine.byId.invalidate()
    },
  })

  return {
    mutate: ({ terminId }: { terminId: string }) => mutation.mutate({ terminId }),
    isPending: mutation.isPending,
  }
}
