/**
 * 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: '/scheduled-tasks',
    name: 'ScheduledTasks',
    component: () => import('@/views/ScheduledTasks.vue'),
    meta: { title: '定时任务' }
  },
  // 功能测试报告（父菜单，含生成报告和ERP配置两个子页面）
  {
    path: '/reports/functional',
    name: 'FunctionalReport',
    component: () => import('@/views/functional-report/index.vue'),
    redirect: '/reports/functional/generate',
    meta: { title: '功能测试报告' },
    children: [
      {
        path: 'generate',
        name: 'FunctionalReportGenerate',
        component: () => import('@/views/functional-report/FunctionalReport.vue'),
        meta: { title: '生成报告' }
      },
      {
        path: 'template-config',
        name: 'TemplateConfig',
        component: () => import('@/views/functional-report/TemplateConfig.vue'),
        meta: { title: '模板配置' }
      },
      {
        path: 'erp-config',
        name: 'ErpConfig',
        component: () => import('@/views/functional-report/ErpConfig.vue'),
        meta: { title: 'ERP配置' }
      }
    ]
  },
  // 设备模拟模块
  {
    path: '/device-sim',
    name: 'DeviceSim',
    component: () => import('@/views/device-sim/index.vue'),
    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: '/deploy',
    name: 'Deploy',
    component: () => import('@/views/deploy/index.vue'),
    redirect: '/deploy/servers',
    meta: { title: '自动化部署测试' },
    children: [
      {
        path: 'servers',
        name: 'DeployServers',
        component: () => import('@/views/deploy/DeployServers.vue'),
        meta: { title: '服务器管理' }
      },
      {
        path: 'scripts',
        name: 'DeployScripts',
        component: () => import('@/views/deploy/DeployScripts.vue'),
        meta: { title: '部署脚本' }
      },
      {
        path: 'executions',
        name: 'DeployExecutions',
        component: () => import('@/views/deploy/DeployExecutions.vue'),
        meta: { title: '部署执行' }
      },
      {
        path: 'executions/:id',
        name: 'DeployExecutionDetail',
        component: () => import('@/views/deploy/DeployExecutionDetail.vue'),
        meta: { title: '执行详情' }
      }
    ]
  },
  // 性能测试模块
  {
    path: '/performance',
    name: 'Performance',
    component: () => import('@/views/performance/index.vue'),
    redirect: '/performance/tasks',
    meta: { title: '性能测试' },
    children: [
      {
        path: 'tasks',
        name: 'PerfTasks',
        component: () => import('@/views/performance/TaskList.vue'),
        meta: { title: '任务管理' }
      },
      {
        path: 'monitor',
        name: 'PerfMonitor',
        component: () => import('@/views/performance/MonitorPanel.vue'),
        meta: { title: '执行监控' }
      },
      {
        path: 'report',
        name: 'PerfReport',
        component: () => import('@/views/performance/ReportPanel.vue'),
        meta: { title: '报告查看' }
      },
      {
        path: 'presets',
        name: 'PerfPresets',
        component: () => import('@/views/performance/ApiPresetList.vue'),
        meta: { title: '接口预设' }
      }
    ]
  },
  // 接口测试模块（一级菜单，含用例管理/执行中心/报告中心）
  {
    path: '/api-test',
    name: 'ApiTest',
    component: () => import('@/views/api-test/index.vue'),
    redirect: '/api-test/cases',
    meta: { title: '接口测试' },
    children: [
      {
        path: 'cases',
        name: 'ApiTestCases',
        component: () => import('@/views/api-test/ApiCaseList.vue'),
        meta: { title: '用例管理' }
      },
      {
        path: 'execution',
        name: 'ApiTestExecution',
        component: () => import('@/views/Execution.vue'),
        meta: { title: '执行中心' }
      },
      {
        path: 'report',
        name: 'ApiTestReport',
        component: () => import('@/views/Reports.vue'),
        meta: { title: '报告中心' }
      }
    ]
  },
  // 重定向：旧版路径 → 性能测试模块
  {
    path: '/cases/performance',
    redirect: '/performance'
  },
  {
    path: '/execution/performance',
    redirect: '/performance'
  },
  {
    path: '/reports/performance',
    redirect: '/performance'
  },
  // 安全测试 ERP 配置（独立子页面，复用 erp_configs 表，与功能测试报告共享配置）
  {
    path: '/security/erp-config',
    name: 'SecurityErpConfig',
    component: () => import('@/views/security/ErpConfig.vue'),
    meta: { title: 'ERP配置' }
  },
  // 安全测试定时任务（复用 scheduled_tasks 表，case_type=security 分流调度）
  {
    path: '/security/scheduled-tasks',
    name: 'SecurityScheduledTasks',
    component: () => import('@/views/security/ScheduledTasks.vue'),
    meta: { title: '定时任务' }
  },
  // 系统管理（父菜单）
  {
    path: '/system',
    name: 'System',
    component: () => import('@/views/system/index.vue'),
    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: '操作日志' }
      }
    ]
  },
  // 文档管理（父菜单，含文档校正优化和文档翻译两个子页面）
  // TODO: 文档管理模块正在开发中，解锁后取消注释
  // {
  //   path: '/document',
  //   name: 'Document',
  //   component: () => import('@/views/document/index.vue'),
  //   redirect: '/document/optimize',
  //   meta: { title: '文档管理' },
  //   children: [
  //     {
  //       path: 'optimize',
  //       name: 'DocumentOptimize',
  //       component: () => import('@/views/document/DocumentOptimize.vue'),
  //       meta: { title: '文档校正优化' }
  //     },
  //     {
  //       path: 'translate',
  //       name: 'DocumentTranslate',
  //       component: () => import('@/views/document/DocumentTranslate.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