import { createRouter, createWebHistory } from 'vue-router'
import { useUserStore } from '@/stores/user'

const router = createRouter({
  history: createWebHistory(),
  routes: [
    {
      path: '/login',
      name: 'login',
      component: () => import('@/views/Login.vue'),
      meta: { requiresAuth: false }
    },
    {
      path: '/',
      name: 'home',
      component: () => import('@/views/Home.vue'),
      meta: { requiresAuth: true }
    },
    {
      path: '/workflow/:id?',
      name: 'workflow',
      component: () => import('@/views/WorkflowDesigner.vue'),
      meta: { requiresAuth: true }
    },
    {
      path: '/executions',
      name: 'executions',
      component: () => import('@/views/Executions.vue'),
      meta: { requiresAuth: true }
    },
    {
      path: '/executions/:id',
      name: 'execution-detail',
      component: () => import('@/views/ExecutionDetail.vue'),
      meta: { requiresAuth: true }
    },
    {
      path: '/agents',
      name: 'agents',
      component: () => import('@/views/Agents.vue'),
      meta: { requiresAuth: true }
    },
    {
      path: '/agents/:id/design',
      name: 'AgentDesigner',
      component: () => import('@/views/WorkflowDesigner.vue'),
      meta: { requiresAuth: true }
    },
    {
      path: '/data-sources',
      name: 'data-sources',
      component: () => import('@/views/DataSources.vue'),
      meta: { requiresAuth: true }
    },
    {
      path: '/model-configs',
      name: 'model-configs',
      component: () => import('@/views/ModelConfigs.vue'),
      meta: { requiresAuth: true }
    },
    {
      path: '/template-market',
      name: 'template-market',
      component: () => import('@/views/TemplateMarket.vue'),
      meta: { requiresAuth: true }
    },
    {
      path: '/permissions',
      name: 'permissions',
      component: () => import('@/views/PermissionManagement.vue'),
      meta: { requiresAuth: true, requiresAdmin: true }
    },
    {
      path: '/monitoring',
      name: 'monitoring',
      component: () => import('@/views/Monitoring.vue'),
      meta: { requiresAuth: true }
    },
    {
      path: '/alert-rules',
      name: 'alert-rules',
      component: () => import('@/views/AlertRules.vue'),
      meta: { requiresAuth: true }
    },
    {
      path: '/node-templates',
      name: 'node-templates',
      component: () => import('@/views/NodeTemplates.vue'),
      meta: { requiresAuth: true }
    }
  ]
})

// 路由守卫
router.beforeEach((to, from, next) => {
  const userStore = useUserStore()
  
  if (to.meta.requiresAuth && !userStore.token) {
    next('/login')
  } else if (to.path === '/login' && userStore.token) {
    next('/')
  } else if (to.meta.requiresAdmin) {
    // 检查管理员权限
    if (!userStore.token) {
      next('/login')
    } else if (!userStore.user) {
      // 如果用户信息未加载，先获取用户信息
      userStore.fetchUser().then(() => {
        if (userStore.user?.role === 'admin') {
          next()
        } else {
          next('/')
        }
      }).catch(() => {
        next('/login')
      })
    } else if (userStore.user.role === 'admin') {
      next()
    } else {
      next('/')
    }
  } else {
    next()
  }
})

export default router
