import { createRouter, createWebHashHistory } from 'vue-router';
import AIDeskCoreInstance from '@tencentcloud/ai-desk-core';

const router = createRouter({
  history: createWebHashHistory(),
  routes: [
    {
      path: '/',
      redirect: '/login',
    },
    {
      path: '/login',
      component: () => import('../views/Login/LoginView.vue'),
    },
    {
      path: '/PCDesk',
      component: () => import('../views/PC/PCDeskView.vue'),
    },
    {
      path: '/mobileDesk',
      component: () => import('../views/Mobile/TabLayout/index.vue'),
      redirect: '/mobileDesk/sessionList',
      children: [
        {
          path: 'sessionList',
          component: () => import('../views/Mobile/SessionsList/index.vue'),
          children: [
            {
              path: 'create',
              component: () =>
                import('../views/Mobile/SessionsList/CreateSession/index.vue'),
              meta: {
                hideBottomTabs: true,
              },
            },
            {
              path: 'search',
              component: () =>
                import('../views/Mobile/SessionsList/SearchSession/index.vue'),
              meta: {
                hideBottomTabs: true,
              },
            },
            {
              path: 'internalContacts',
              component: () =>
                import('../views/Mobile/SessionsList/InternalContacts/index.vue'),
              meta: {
                hideBottomTabs: true,
              },
            },
            {
              path: 'externalContacts',
              component: () =>
                import('../views/Mobile/SessionsList/ExternalContacts/index.vue'),
              meta: {
                hideBottomTabs: true,
              },
            },
          ],
        },
        {
          path: 'serviceRecord',
          component: () => import('../views/Mobile/ServiceRecord/index.vue'),
        },
        {
          path: 'mine',
          component: () => import('../views/Mobile/Mine/index.vue'),
          children: [
            {
              path: 'changePassword',
              component: () =>
                import('../views/Mobile/Mine/ChangePassword/index.vue'),
              meta: {
                hideBottomTabs: true,
              },
            },
            {
              path: 'changePhone',
              component: () =>
                import('../views/Mobile/Mine/ChangePhone/index.vue'),
              meta: {
                hideBottomTabs: true,
              },
            },
          ],
        },
      ],
    },
    {
      path: '/mobileDesk/chat/:sessionId',
      component: () => import('../views/Mobile/Chat/index.vue'),
      children: [
        {
          path: 'transfer',
          component: () =>
            import('../views/Mobile/Chat/ChatTransfer/index.vue'),
        },
        {
          path: 'profile',
          component: () => import('../views/Mobile/Chat/ChatProfile/index.vue'),
        },
        {
          path: 'userDefined',
          component: () => import('../views/Mobile/Chat/UserDefined/index.vue'),
        },
      ],
    },
  ],
});

router.beforeEach((to) => {
  if (to.path === '/login') {
    return true;
  }

  if (!AIDeskCoreInstance.isReady()) {
    return '/login';
  }

  return true;
});

export default router;
