import { useQuery, useMutation, useQueryClient } from '@tanstack/react-query';
import * as api from '@/lib/api/client-invitations';

export function useCreateInvitation() {
  const qc = useQueryClient();
  return useMutation({
    mutationFn: api.createInvitation,
    onSuccess: () => qc.invalidateQueries({ queryKey: ['client-invitations'] }),
  });
}

export function useValidateInvitationToken(token: string) {
  return useQuery({
    queryKey: ['invitation-token', token],
    queryFn: () => api.validateInvitationToken(token),
    enabled: !!token,
    retry: false,
  });
}

export function useRegisterFromInvitation() {
  return useMutation({
    mutationFn: ({ token, data }: { token: string; data: Parameters<typeof api.registerFromInvitation>[1] }) =>
      api.registerFromInvitation(token, data),
  });
}

export function useResendInvitation() {
  const qc = useQueryClient();
  return useMutation({
    mutationFn: api.resendInvitation,
    onSuccess: () => qc.invalidateQueries({ queryKey: ['client-invitations'] }),
  });
}

export function useClientInvitations() {
  return useQuery({
    queryKey: ['client-invitations'],
    queryFn: api.getClientInvitations,
  });
}
