import { createRouter, createWebHistory, RouteRecordRaw } from 'vue-router'
import { sessionStorage } from '@/plugins/storage'
import Bootstrap from './/bootstrap'
import AnalysisRoutes from './analysis'
import MonitorRoutes from './monitor'
import MarkingRoutes from './marking'
import ExampleRoutes from './example'
import ExpertRoutes from './expert'
import QualityRoutes from './quality'
import AdminRoutes from './admin'

export const routes: RouteRecordRaw[] = [
  ...Bootstrap,
  ...AnalysisRoutes,
  ...MonitorRoutes,
  ...MarkingRoutes,
  ...ExpertRoutes,
  ...QualityRoutes,
  ...AdminRoutes,
  {
    name: 'Error',
    path: '/:path(.*)*',
    component: () => import('@/modules/error/404.vue'),
  },
]

if (import.meta.env.DEV) {
  routes.unshift(...ExampleRoutes)
}

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

router.beforeEach((to, from, next) => {
  if (to.meta?.auth === false || to.name === 'Login' || sessionStorage.get('LOGIN_RESULT')) {
    next()
  } else {
    next({ name: 'Login' })
  }
})

export default router
