import { useEffect } from 'react';
import { useAuthStore } from '@/store/authStore';
import { useCurrentUser } from '@/api/hooks';

export function useAuth() {
  const store = useAuthStore();
  const { data, isLoading, isError } = useCurrentUser();

  useEffect(() => {
    if (isError) {
      store.setAuthenticated(false);
      store.setUser(null);
    }
  }, [isError, store]);

  return {
    user: store.user,
    isAuthenticated: store.isAuthenticated,
    isLoading: isLoading && !store.user,
    currentTenant: store.currentTenant,
  };
}
