/**
 * Vue Router 配置
 *
 * @author czj
 * @date 2026-07-09
 */

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

/**
 * 路由配置
 */
const routes: RouteRecordRaw[] = [
  {
    path: '/',
    name: 'Dashboard',
    component: () => import('@/views/Dashboard.vue'),
    meta: { title: '测试总览' }
  },
  {
    path: '/modules/:type?',
    name: 'Modules',
    component: () => import('@/views/Modules.vue'),
    meta: { title: '模块管理' }
  },
  {
    path: '/cases/:type?',
    name: 'Cases',
    component: () => import('@/views/Cases.vue'),
    meta: { title: '用例管理' }
  },
  {
    path: '/recorder',
    name: 'Recorder',
    component: () => import('@/views/Recorder.vue'),
    meta: { title: '用例录制' }
  },
  {
    path: '/execution/:type?',
    name: 'Execution',
    component: () => import('@/views/Execution.vue'),
    meta: { title: '执行中心' }
  },
  {
    path: '/reports/:type?',
    name: 'Reports',
    component: () => import('@/views/Reports.vue'),
    meta: { title: '报告中心' }
  },
  // 设备模拟模块
  {
    path: '/device-sim',
    name: 'DeviceSim',
    redirect: '/device-sim/settings',
    meta: { title: '设备模拟' },
    children: [
      {
        path: 'settings',
        name: 'EnvConfig',
        component: () => import('@/views/device-sim/EnvConfig.vue'),
        meta: { title: '环境配置' }
      },
      {
        path: 'door',
        name: 'DoorSim',
        component: () => import('@/views/device-sim/DoorSim.vue'),
        meta: { title: '门口屏模拟' }
      },
      {
        path: 'paperless',
        name: 'PaperlessSim',
        component: () => import('@/views/device-sim/PaperlessSim.vue'),
        meta: { title: '无纸化模拟' }
      },
      {
        path: 'central',
        name: 'CentralSim',
        component: () => import('@/views/device-sim/CentralSim.vue'),
        meta: { title: '中控设备模拟' }
      },
      {
        path: 'client',
        name: 'ClientSim',
        component: () => import('@/views/device-sim/ClientSim.vue'),
        meta: { title: '集控客户端模拟' }
      }
    ]
  },
  // 系统管理（父菜单）
  {
    path: '/system',
    name: 'System',
    redirect: '/system/settings',
    meta: { title: '系统管理' },
    children: [
      {
        path: 'settings',
        name: 'SystemSettings',
        component: () => import('@/views/system/Settings.vue'),
        meta: { title: '系统配置' }
      },
      {
        path: 'info',
        name: 'SystemInfo',
        component: () => import('@/views/system/Info.vue'),
        meta: { title: '系统信息' }
      },
      {
        path: 'logs',
        name: 'OperationLogs',
        component: () => import('@/views/system/Logs.vue'),
        meta: { title: '操作日志' }
      }
    ]
  },
  // 旧 /settings 路由 → 重定向到 /system/settings
  {
    path: '/settings',
    redirect: '/system/settings'
  }
]

/**
 * 创建路由实例
 */
const router = createRouter({
  history: createWebHistory(),
  routes
})

/**
 * 路由守卫 - 设置页面标题
 */
router.beforeEach((to, _from, next) => {
  // 设置页面标题
  const title = to.meta?.title as string
  if (title) {
    document.title = `${title} - 测试管理平台`
  }
  next()
})

export default router