import type { NavigationGuardWithThis } from 'vue-router';
import { authService } from './auth.service';
import { useAuthStore } from '@/modules/auth/auth.store';

export const authGuard: NavigationGuardWithThis<undefined> = async (_to, _from) => {
  if (!authService.isAuthenticated()) {
    return { name: 'login' };
  }

  const authStore = useAuthStore();
  if (!authStore.currentUser) {
    await authStore.fetchMe();
  }

  return true;
};
