import { createRouter, createWebHistory } from 'vue-router'

const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: [
    {
      path: '/',
      name: 'home',
      component: () => import('../pages/Home/index.vue'),
    },
    {
      path: '/user-center',
      name: 'user-center',
      component: () => import('../pages/UserCenter/index.vue'),
    },
    {
      path: '/terms',
      name: 'terms-of-service',
      component: () => import('../pages/TermsOfService/index.vue'),
    },
    {
      path: '/privacy',
      name: 'privacy-policy',
      component: () => import('../pages/PrivacyPolicy/index.vue'),
    },
    {
      path: '/login/:token',
      name: 'login-redirect',
      component: () => import('../pages/LoginRedirect/index.vue'),
    },
    {
      path: '/:pathMatch(.*)*',
      name: 'not-found',
      redirect: '/',
    },
  ],
})

router.beforeEach((to, _from, next) => {
  if (to.path === '/user-center' && !localStorage.getItem('Authorization')) {
    next({ path: '/', query: { login: 'true' } })
  } else {
    next()
  }
})

export default router
