/**
 * 前端路由配置
 * 定义所有页面路由，包括文档检索、研究分析、知识图谱、管理后台等模块。
 * 使用 HTML5 History 模式，所有页面组件均按需懒加载。
 */
import { createRouter, createWebHistory, type RouteRecordRaw } from 'vue-router'

const routes: RouteRecordRaw[] = [
  {
    path: '/',
    redirect: '/search',
  },
  {
    path: '/search',
    name: 'Search',
    component: () => import('@/views/SearchView.vue'),
    meta: { title: '文档检索', menuKey: 'search' },
  },
  {
    path: '/research',
    name: 'ResearchList',
    component: () => import('@/views/ResearchListView.vue'),
    meta: { title: '研究记录', menuKey: 'research' },
  },
  {
    path: '/research/new',
    name: 'ResearchCreate',
    component: () => import('@/views/ResearchCreateView.vue'),
    meta: { title: '新建研究', menuKey: 'research' },
  },
  {
    path: '/research/:id',
    name: 'ResearchDetail',
    component: () => import('@/views/ResearchDetailView.vue'),
    meta: { title: '研究详情', menuKey: 'research' },
  },
  {
    path: '/research/:id/compare/:otherId',
    name: 'ResearchCompare',
    component: () => import('@/views/ResearchCompareView.vue'),
    meta: { title: '版本对比', menuKey: 'research' },
  },
  {
    path: '/qa',
    name: 'QA',
    component: () => import('@/views/QAView.vue'),
    meta: { title: '智能问答', menuKey: 'qa' },
  },
  {
    path: '/notebook',
    name: 'NotebookList',
    component: () => import('@/views/NotebookListView.vue'),
    meta: { title: '专题写稿', menuKey: 'notebook' },
  },
  {
    path: '/notebook/:id',
    name: 'NotebookDetail',
    component: () => import('@/views/NotebookView.vue'),
    meta: { title: '专题写稿', menuKey: 'notebook' },
  },
  {
    path: '/doc/:id',
    name: 'DocDetail',
    component: () => import('@/views/DocDetailView.vue'),
    meta: { title: '文档详情', menuKey: 'search' },
  },
  {
    path: '/matters',
    name: 'Matters',
    component: () => import('@/views/MattersView.vue'),
    meta: { title: '事项查询', menuKey: 'matters' },
  },
  {
    path: '/guides',
    name: 'ServiceGuides',
    component: () => import('@/views/ServiceGuidesView.vue'),
    meta: { title: '办事指南', menuKey: 'guides' },
  },
  {
    path: '/guides/by-doc/:docId',
    name: 'GuideByDoc',
    component: () => import('@/views/GuideDetailView.vue'),
    meta: { title: '指南详情', menuKey: 'guides' },
  },
  {
    path: '/guides/:profileId',
    name: 'GuideDetail',
    component: () => import('@/views/GuideDetailView.vue'),
    meta: { title: '指南详情', menuKey: 'guides' },
  },
  {
    path: '/matters/:matterId',
    name: 'MatterDetail',
    component: () => import('@/views/MatterDetailView.vue'),
    meta: { title: '事项详情', menuKey: 'matters' },
  },
  {
    path: '/graph',
    name: 'GraphExplorer',
    component: () => import('@/views/GraphExplorer.vue'),
    meta: { title: '知识图谱', menuKey: 'graph' },
  },
  {
    path: '/admin',
    name: 'AdminDashboard',
    component: () => import('@/views/admin/Dashboard.vue'),
    meta: { title: '管理后台', menuKey: 'admin' },
  },
  {
    path: '/admin/graph',
    name: 'AdminGraphManager',
    component: () => import('@/views/admin/GraphManager.vue'),
    meta: { title: '图谱管理', menuKey: 'admin' },
  },
  {
    path: '/admin/ingest-traces',
    name: 'AdminIngestTraces',
    component: () => import('@/views/admin/IngestTraces.vue'),
    meta: { title: '入库追踪', menuKey: 'admin' },
  },
  {
    path: '/admin/convert-logs',
    name: 'AdminConvertLogs',
    component: () => import('@/views/admin/ConvertLogs.vue'),
    meta: { title: '转换记录', menuKey: 'admin' },
  },
  {
    path: '/mock',
    name: 'MockOA',
    component: () => import('@/views/MockOA.vue'),
    meta: { title: 'Mock OA 测试台', menuKey: 'mock' },
  },
  // 404 兜底路由：未匹配的路径重定向到搜索页
  // Catch-all route: redirect unmatched paths to search page
  {
    path: '/:pathMatch(.*)*',
    name: 'NotFound',
    redirect: '/search',
    meta: { title: '页面未找到' },
  },
]

const router = createRouter({
  history: createWebHistory(),
  routes,
  // 路由切换后自动滚动到页面顶部
  // Scroll to top on route change
  scrollBehavior: () => ({ top: 0 }),
})

// 全局前置守卫：根据路由 meta.title 动态设置浏览器标签页标题
router.beforeEach((to, _from, next) => {
  const title = (to.meta.title as string) || '政务文档智能检索系统'
  document.title = `${title} - 政务文档智能检索系统`
  next()
})

export default router
