import { createRouter, createWebHistory } from 'vue-router'
import PortalLayout from '../layouts/PortalLayout.vue'
import AdminLayout from '../layouts/AdminLayout.vue'
import Login from '../views/Login.vue'
import Register from '../views/Register.vue'
import { setupGuards } from './setupGuards'

export const ROUTE_NAMES = {
  AdminRoot: 'AdminRoot',
  AdminLayout: 'AdminLayout',
}

const routes = [
  { path: '/', redirect: '/portal/home' },
  { path: '/login', component: Login },
  { path: '/register', component: Register },
  {
    path: '/portal',
    component: PortalLayout,
    children: [
      { path: '', redirect: '/portal/home' },
      { path: 'home', component: () => import('../views/portal/Home.vue') },
      { path: 'monitor', component: () => import('../views/portal/Monitor.vue') },
      { path: 'chat', component: () => import('../views/portal/Chat.vue') },
      { path: 'about', component: () => import('../views/portal/About.vue') },
      { path: 'profile', component: () => import('../views/Profile.vue') },
      {
        path: '403',
        component: () => import('../components/Error.vue'),
        props: {
          code: 403,
          title: '抱歉，您没有权限访问该页面',
          subTitle: '请登录账号或联系管理员开通权限，或切换账号后重试。',
        },
      },
      {
        path: ':pathMatch(.*)*',
        component: () => import('../components/Error.vue'),
        props: { code: 404 },
      },
    ],
  },
  {
    path: '/admin',
    name: ROUTE_NAMES.AdminLayout,
    component: AdminLayout,
    children: [
      { path: '', name: ROUTE_NAMES.AdminRoot, redirect: '/admin/dashboard' },
      { path: 'dashboard', component: () => import('../views/admin/Dashboard.vue') },
      { path: 'ai/knowledge-base', component: () => import('../views/admin/ai/KnowledgeBase.vue') },
      { path: 'profile', component: () => import('../views/Profile.vue') },
      {
        path: '403',
        component: () => import('../components/Error.vue'),
        props: {
          code: 403,
          title: '抱歉，您没有权限访问该页面',
          subTitle: '请联系管理员开通权限，或切换账号后重试。',
        },
      },
      {
        path: ':pathMatch(.*)*',
        component: () => import('../components/Error.vue'),
        props: { code: 404 },
      },
    ],
  },
  {
    path: '/:pathMatch(.*)*',
    component: PortalLayout,
    children: [
      {
        path: '',
        component: () => import('../components/Error.vue'),
        props: { code: 404 },
      },
    ],
  },
]

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

setupGuards(router)

export default router
