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

const routes: RouteRecordRaw[] = [
  // 用户端路由
  {
    path: '/',
    name: 'Interview',
    component: () => import('@/layouts/InterviewLayout.vue'),
    children: [
      {
        path: '',
        name: 'Welcome',
        component: () => import('@/pages/interview/index.vue'),
        meta: { title: '欢迎' },
      },
      {
        path: 'info',
        name: 'InfoCollection',
        component: () => import('@/pages/interview/info.vue'),
        meta: { title: '信息填写' },
      },
      {
        path: 'call',
        name: 'Call',
        component: () => import('@/pages/interview/call.vue'),
        meta: { title: '语音面试' },
      },
      {
        path: 'complete',
        name: 'Complete',
        component: () => import('@/pages/interview/complete.vue'),
        meta: { title: '面试完成' },
      },
    ],
  },
  // 管理后台路由
  {
    path: '/admin/login',
    name: 'AdminLogin',
    component: () => import('@/pages/admin/login.vue'),
    meta: { title: '管理员登录' },
  },
  {
    path: '/admin',
    name: 'Admin',
    component: () => import('@/pages/admin/layout.vue'),
    redirect: '/admin/dashboard',
    children: [
      {
        path: 'dashboard',
        name: 'Dashboard',
        component: () => import('@/pages/admin/dashboard.vue'),
        meta: { title: '数据概览' },
      },
      {
        path: 'interviews',
        name: 'InterviewList',
        component: () => import('@/pages/admin/interviews.vue'),
        meta: { title: '面试列表' },
      },
      {
        path: 'interviews/:id',
        name: 'InterviewDetail',
        component: () => import('@/pages/admin/interview-detail.vue'),
        meta: { title: '面试详情' },
      },
      {
        path: 'configs',
        name: 'Configs',
        component: () => import('@/pages/admin/configs.vue'),
        meta: { title: '配置管理' },
      },
    ],
  },
]

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

// 路由守卫 - 设置页面标题
router.beforeEach((to, _from, next) => {
  const title = to.meta.title as string
  document.title = title ? `${title} - AI面试助手` : 'AI面试助手'
  next()
})

export default router
