import Login from '../views/authentication/Login.vue';
import Register from '../views/authentication/Register.vue';
import ManageProfile from '../views/authentication/ManageProfile.vue';
import Profile from '../views/authentication/Profile.vue';
import store from '../store';
import {createRouter, createWebHistory} from "vue-router";

const routes = [
  { path: '/login', component: Login },
  { path: '/register', component: Register },
  { path: '/manageProfile', component: ManageProfile },
  {
    path: '/profile',
    component: Profile,
    meta: { requiresAuth: true },
  },
];

const router = createRouter({
  history: createWebHistory(),
  routes,
});

router.beforeEach((to, from, next) => {
  if (to.matched.some(record => record.meta.requiresAuth)) {
    if (!store.getters['authentication/isAuthenticated']) {
      next('/login');
    } else {
      next();
    }
  } else {
    next();
  }
});

export default routes;
