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

// 定义路由
const routes: Array<RouteRecordRaw> = [
  {
    path: '/',
    name: 'Home',
    component: () => import('../views/HomeView.vue'),
    meta: {
      title: '首页'
    }
  },
  {
    path: '/algorithms',
    name: 'Algorithms',
    component: () => import('../views/AlgorithmsView.vue'),
    meta: {
      title: '算法列表'
    }
  },
  {
    path: '/algorithm/:id',
    name: 'AlgorithmDetail',
    component: () => import('../views/AlgorithmDetailView.vue'),
    meta: {
      title: '算法详情'
    }
  },
  {
    path: '/algorithm/:id/call',
    name: 'AlgorithmCall',
    component: () => import('../views/AlgorithmCallView.vue'),
    meta: {
      title: '算法调用'
    }
  },
  {
    path: '/algorithm/:id/versions',
    name: 'AlgorithmVersions',
    component: () => import('../views/AlgorithmVersionsView.vue'),
    meta: {
      title: '算法版本管理'
    }
  },
  {
    path: '/admin',
    name: 'Admin',
    component: () => import('../views/AdminView.vue'),
    meta: {
      title: '管理员中心',
      requiresAuth: true,
      requiresAdmin: true
    },
    children: [
      {
        path: 'algorithms',
        name: 'AdminAlgorithms',
        component: () => import('../views/admin/AdminAlgorithmsView.vue'),
        meta: {
          title: '算法仓库管理'
        }
      },
      {
        path: 'services',
        name: 'AdminAlgorithmServices',
        component: () => import('../views/admin/AdminAlgorithmServicesView.vue'),
        meta: {
          title: '算法服务管理'
        }
      },
      {
        path: 'users',
        name: 'AdminUsers',
        component: () => import('../views/admin/AdminUsersView.vue'),
        meta: {
          title: '用户管理'
        }
      },
      {
        path: 'api-keys',
        name: 'AdminApiKeys',
        component: () => import('../views/admin/AdminApiKeysView.vue'),
        meta: {
          title: 'API密钥管理'
        }
      },
      {
        path: 'api',
        name: 'AdminApiManagement',
        component: () => import('../views/admin/AdminApiManagementView.vue'),
        meta: {
          title: 'API管理'
        }
      },
      {
        path: 'service-registration',
        name: 'AdminServiceRegistration',
        component: () => import('../views/admin/AdminServiceRegistrationView.vue'),
        meta: {
          title: '服务注册'
        }
      }
    ]
  },
  {
    path: '/login',
    name: 'Login',
    component: () => import('../views/LoginView.vue'),
    meta: {
      title: '登录'
    }
  },
  {
    path: '/register',
    name: 'Register',
    component: () => import('../views/RegisterView.vue'),
    meta: {
      title: '注册'
    }
  },
  // 404页面
  {
    path: '/:pathMatch(.*)*',
    name: 'NotFound',
    component: () => import('../views/NotFoundView.vue'),
    meta: {
      title: '页面不存在'
    }
  }
]

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

// 路由守卫，用于设置页面标题和权限控制
router.beforeEach((to, _from, next) => {
  // 设置页面标题
  if (to.meta.title) {
    document.title = `${to.meta.title} - 智能算法展示平台`
  }
  
  // 权限控制
  if (to.meta.requiresAuth) {
    const token = localStorage.getItem('token')
    const user = localStorage.getItem('user')
    
    if (!token || !user) {
      next({ name: 'Login' })
      return
    }
    
    // 检查是否需要管理员权限
    if (to.meta.requiresAdmin) {
      try {
        const userObj = JSON.parse(user)
        if (userObj.role !== 'admin') {
          next({ name: 'Home' })
          return
        }
      } catch {
        next({ name: 'Login' })
        return
      }
    }
  }
  
  next()
})

export default router
