import { useMutation, UseMutationOptions } from '@tanstack/react-query';
import { useInject } from '@/lib/di/hooks/useInject';
import { AUTH_SERVICE_TOKEN } from '@/lib/di/tokens';
import { ApiError } from '@/lib/api/base/ApiError';
import { SessionViewModel } from '@/lib/view-models/SessionViewModel';
import type { SignupParamsDTO } from '@/lib/types/generated/SignupParamsDTO';

export function useSignup(
  options?: Omit<UseMutationOptions<SessionViewModel, ApiError, SignupParamsDTO>, 'mutationFn'>
) {
  const authService = useInject(AUTH_SERVICE_TOKEN);

  return useMutation<SessionViewModel, ApiError, SignupParamsDTO>({
    mutationFn: async (params) => {
      const result = await authService.signup(params);
      if (result.isErr()) {
        throw result.getError();
      }
      return result.unwrap();
    },
    ...options,
  });
}