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

declare module 'vue-router' {
  interface RouteMeta {
    requiresAuth?: boolean
    roles?: string[]
    title?: string
  }
}

const routes: RouteRecordRaw[] = [
  {
    path: '/login',
    name: 'Login',
    component: () => import('@/views/system/Login.vue'),
    meta: { title: '登录' }
  },
  {
    path: '/403',
    name: 'Forbidden',
    component: () => import('@/views/system/Forbidden.vue'),
    meta: { title: '无权限' }
  },
  {
    path: '/',
    component: () => import('@/layouts/DefaultLayout.vue'),
    redirect: '/dashboard',
    meta: { requiresAuth: true },
    children: [
      {
        path: 'dashboard',
        name: 'Dashboard',
        component: () => import('@/views/system/Dashboard.vue'),
        meta: { title: '仪表盘' }
      },
      {
        path: 'users',
        name: 'UserManagement',
        component: () => import('@/views/system/UserManagement.vue'),
        meta: { title: '用户管理' }
      },
      {
        path: 'roles',
        name: 'RoleManagement',
        component: () => import('@/views/system/RoleManagement.vue'),
        meta: { title: '角色管理' }
      },
      {
        path: 'menus',
        name: 'MenuManagement',
        component: () => import('@/views/system/MenuManagement.vue'),
        meta: { title: '菜单管理' }
      },
      {
        path: 'sys/config',
        name: 'ConfigManagement',
        component: () => import('@/views/config/ConfigManagement.vue'),
        meta: { title: '参数配置' }
      },
      {
        path: 'dict',
        name: 'DictManagement',
        component: () => import('@/views/config/DictManagement.vue'),
        meta: { title: '字典管理' }
      },
      {
        path: 'files',
        name: 'FileManagement',
        component: () => import('@/views/file/FileManagement.vue'),
        meta: { title: '文件管理' }
      },
      {
        path: 'notice',
        name: 'NoticeManagement',
        component: () => import('@/views/notify/NoticeManagement.vue'),
        meta: { title: '通知公告' }
      },
      {
        path: 'loginlog',
        name: 'LoginLog',
        component: () => import('@/views/audit/LoginLog.vue'),
        meta: { title: '登录日志' }
      },
      {
        path: 'oplog',
        name: 'OperationLog',
        component: () => import('@/views/audit/OperationLog.vue'),
        meta: { title: '操作日志' }
      },
      {
        path: 'exceptionlog',
        name: 'ExceptionLog',
        component: () => import('@/views/audit/ExceptionLog.vue'),
        meta: { title: '异常日志' }
      }
    ]
  }
]

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

function checkRoutePermission(route: RouteLocationNormalized, userRoles: string[]): boolean {
  if (!route.meta.roles || !Array.isArray(route.meta.roles) || route.meta.roles.length === 0) {
    return true
  }
  return route.meta.roles.some((role: string) => userRoles.includes(role))
}

router.beforeEach((to, _from, next) => {
  try {
    const token = localStorage.getItem('token')
    const rolesStr = localStorage.getItem('roles')
    let userRoles: string[] = []
    
    try {
      userRoles = rolesStr ? JSON.parse(rolesStr) : []
    } catch (e) {
      console.warn('解析用户角色失败，将使用空数组:', e)
      userRoles = []
    }
    
    if (to.meta.title) {
      document.title = `${to.meta.title} - Novalon 管理系统`
    }
    
    if (to.path === '/login') {
      if (token) {
        next('/')
      } else {
        next()
      }
    } else if (to.path === '/403') {
      next()
    } else {
      if (to.meta.requiresAuth !== false && !token) {
        next('/login')
        return
      }
      
      if (!checkRoutePermission(to, userRoles)) {
        console.warn(`用户角色 ${userRoles} 无权访问路由 ${to.path}，需要角色: ${to.meta.roles}`)
        next('/403')
        return
      }
      
      next()
    }
  } catch (error) {
    console.error('路由守卫错误:', error)
    next('/login')
  }
})

export default router
