import { useMutation, useQuery } from '@tanstack/react-query';
import { trpc } from '@/trpc';
import type { InferProcedureOutput } from '@/server/trpc/types';

/**
 * Hook for subscribing to the waitlist
 */
export function useWaitlistSignup() {
  return useMutation<
    InferProcedureOutput<AppRouter, 'waitlistRouter.signup'>,
    Error,
    {
      email: string;
      name?: string;
      source?: string;
      referralCode?: string;
    }
  >({
    mutationFn: async (input) => {
      return trpc.waitlistRouter.signup.mutateAsync(input);
    },
  });
}

/**
 * Hook for getting the total waitlist count
 */
export function useWaitlistCount() {
  return useQuery<
    InferProcedureOutput<AppRouter, 'waitlistRouter.getCount'>,
    Error
  >({
    queryKey: ['waitlistCount'],
    queryFn: () => trpc.waitlistRouter.getCount.queryAsync(),
  });
}

/**
 * Hook for getting referral count for a specific code
 */
export function useReferralCount(referralCode: string) {
  return useQuery<
    InferProcedureOutput<AppRouter, 'waitlistRouter.getReferralCount'>,
    Error
  >({
    queryKey: ['referralCount', referralCode],
    queryFn: () => trpc.waitlistRouter.getReferralCount.queryAsync({ referralCode }),
    enabled: !!referralCode,
  });
}
