import { createRouter, createWebHistory } from 'vue-router'
import type { RouteRecordRaw } from 'vue-router'
import Layout from '@/layout/index.vue'

/**
 * 静态路由：无需登录即可访问
 * 动态路由通过 router.addRoute('Root', route) 在运行时注入
 */
export const constantRoutes: RouteRecordRaw[] = [
  {
    path: '/auth',
    name: 'auth',
    component: () => import('@/views/auth/index.vue'),
    meta: { hidden: true, title: '登录' },
  },
  {
    // Root Layout：所有需要登录的页面都是它的子路由
    path: '/',
    name: 'Root', // ← 重要：addRoute 时通过 name 定位父路由
    component: Layout,
    redirect: '/dashboard',
    children: [], // 动态路由在此注入
  },
  // 注意：404 路由移到动态添加，避免先于权限路由匹配
]

/**
 * 404 路由（动态添加，确保在权限路由之后）
 */
export const notFoundRoute: RouteRecordRaw = {
  path: '/:pathMatch(.*)*',
  name: 'NotFound',
  component: () => import('@/views/error/404.vue'),
  meta: { hidden: true, title: '404' },
}

const router = createRouter({
  history: createWebHistory(),
  routes: constantRoutes,
  scrollBehavior: () => ({ top: 0 }),
})

export default router
