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';

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

  return useMutation<void, ApiError, void>({
    mutationFn: async () => {
      const result = await authService.logout();
      if (result.isErr()) {
        const error = result.getError();
        throw new ApiError(error.message, 'SERVER_ERROR', { timestamp: new Date().toISOString() });
      }
    },
    ...options,
  });
}