import { useMutation } from '@tanstack/react-query';
import { authApi } from '../api/authApi';
import { useAuthStore } from '../store/authStore';
import { RegisterCredentials, AuthResponse } from '../types/index';

export const useRegister = () => {
  const loginStore = useAuthStore((state) => state.login);

  return useMutation<AuthResponse, Error, RegisterCredentials>({
    mutationFn: authApi.register,
    onSuccess: (data) => {
      // Backend returns user + token on register, so we can auto-login
      loginStore(data);
    },
  });
};