import { useMutation, useQueryClient } from '@tanstack/react-query';
import { useNavigate } from 'react-router-dom';
import type { RegisterParams } from '@/client/models/RegisterParams';
import { AuthService } from '@/client/services/AuthService';

export function useRegister() {
  const queryClient = useQueryClient();
  const navigate = useNavigate();

  return useMutation({
    mutationFn: (params: RegisterParams) => AuthService.authRegister({ requestBody: params }),
    onSuccess: () => {
      queryClient.invalidateQueries({ queryKey: ['auth', 'me'] });
      navigate('/');
    },
  });
}
