import { createRouter, createWebHistory } from 'vue-router';
import store from '../store';
import HomeView from '../views/HomeView.vue';
import authRoutes from './authRoutes';
import socialRoutes from './socialRoutes';
import settingsRoutes from './settingsRoutes';
import adminRoutes from './adminRoutes';
import falukantRoutes from './falukantRoutes';
import blogRoutes from './blogRoutes';
import minigamesRoutes from './minigamesRoutes';

const routes = [
  {
    path: '/',
    name: 'Home',
    component: HomeView
  },
  ...authRoutes,
  ...socialRoutes,
  ...settingsRoutes,
  ...adminRoutes,
  ...falukantRoutes,
  ...blogRoutes,
  ...minigamesRoutes,
];

const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes
});

router.beforeEach((to, from, next) => {
  if (to.matched.some(record => record.meta.requiresAuth)) {
    if (!store.getters.isLoggedIn) {
      next('/');
    } else if (!store.getters.user.active) {
      next('/activate');
    } else {
      next();
    }
  } else {
    next();
  }
});

export default router;
