import { createRouter, createWebHistory } from 'vue-router'
import LoginView from '@/views/LoginView.vue'
import MainView from '@/views/MainView.vue'
import userInfo from '@/components/userInfo'

const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: [
    {
      path: '/login.html',
      name: 'login',
      component: LoginView,
      meta: { isRequiredAuth: false },
    },
    {
      path: '/main.html',
      name: 'main',
      component: MainView,
      props: { contentId: 'dashboard-view' },
      meta: { isRequiredAuth: true },
    },
    {
      path: '/agent-manage.html',
      name: 'agent-manage',
      component: MainView,
      props: { contentId: 'agent-manage-view' },
      meta: { isRequiredAuth: true },
    },
    {
      path: '/message-history.html',
      name: 'message-history',
      component: MainView,
      props: { contentId: 'message-history-view' },
      meta: { isRequiredAuth: true },
    },
  ],
})

router.beforeEach((to, from, next) => {
  if (to.meta.isRequiredAuth && !userInfo.isLogin) {
    next('/login.html')
  } else if ((to.path === '/login.html' || to.path === '/') && userInfo.isLogin) {
    next('/main.html')
  } else {
    next()
  }
})

export default router
