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

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

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