import { createRouter, createWebHashHistory, RouterView } from 'vue-router'
import appRouters from './app-routers'
import { useCoreStore } from '@/stores'
import HomeView from '@/views/HomeView.vue'

const { DEV } = import.meta.env

const router = createRouter({
  history: createWebHashHistory(),
  routes: [
    {
      path: '/',
      component: RouterView,
      children: [
        {
          path: '/',
          redirect: '/home'
        },
        {
          path: '/home',
          component: HomeView,
          name: 'home',
          meta: {
            title: '首页',
            keepAlive: true
          }
        },
        // 应用中其他路由
        ...appRouters,
        {
          path: 'error',
          name: 'error',
          component: () => import('@/views/ErrorView.vue'),
          meta: {
            title: '错误页面',
            keepAlive: true
          }
        },
        {
          path: ':pathMatch(.*)*',
          name: 'NotFound',
          component: () => import('@/views/ErrorView.vue'),
          meta: {
            title: '404 Not Found',
            keepAlive: true
          }
        }
      ]
    }
  ]
})

// 全局路由守卫(登录拦截)
router.beforeEach(async (to, from, next) => {
  // 当path为/error的时候，不去校验是否有用户信息
  if (DEV || to.path === '/error') {
    next()
    return
  }
  const coreStore = useCoreStore()
  await coreStore.init()
  const user = coreStore.currentUser
  if (!user) {
    next({
      path: '/error', // login
      query: {
        redirect: to.fullPath
      }
    })
    return
  }

  if (DEV || !to.meta?.permissions) {
    next()
    return
  }

  const permissions: Array<string> = (to.meta.permissions as string).split(',')
  const authorities = coreStore.userAuthority?.permissions ?? []
  if (authorities.some((a) => permissions.includes(a.code))) {
    next()
  } else {
    next({
      path: '/error'
    })
  }
})

export default router
