import { createRouter, createWebHistory } from 'vue-router'
import { useAuthStore } from '../../shared/stores/auth'

const router = createRouter({
  history: createWebHistory(),
  routes: [
    {
      path: '/login',
      name: 'Login',
      component: () => import('../../views/auth/Login.vue'),
      meta: { requiresAuth: false }
    },
    {
      path: '/',
      component: () => import('../layouts/AppLayout.vue'),
      meta: { requiresAuth: true },
      children: [
        {
          path: '',
          name: 'Dashboard',
          component: () => import('../../views/Dashboard.vue')
        },
        {
          path: 'agents',
          name: 'AgentList',
          component: () => import('../../views/agents/AgentList.vue')
        },
        {
          path: 'agents/:id',
          name: 'AgentDetail',
          component: () => import('../../views/agents/AgentDetail.vue')
        },
        {
          path: 'nodes',
          name: 'NodeList',
          component: () => import('../../views/nodes/NodeList.vue')
        },
        {
          path: 'nodes/:name',
          name: 'NodeDetail',
          component: () => import('../../views/nodes/NodeDetail.vue')
        },
        {
          path: 'local-jobs',
          name: 'LocalJobList',
          component: () => import('../../views/localJobs/LocalJobList.vue')
        },
        {
          path: 'local-jobs/:id',
          name: 'LocalJobDetail',
          // @ts-ignore
          component: () => import('../../views/localJobs/LocalJobDetail.vue')
        },
        {
          path: 'flows',
          name: 'FlowBuilder',
          component: () => import('../../views/flows/FlowBuilder.vue')
        },
        {
          path: 'scheduled-jobs',
          name: 'ScheduledJobList',
          component: () => import('../../views/scheduledJobs/ScheduledJobList.vue')
        },
        {
          path: 'scheduled-jobs/:id',
          name: 'ScheduledJobDetail',
          component: () => import('../../views/scheduledJobs/ScheduledJobDetail.vue')
        },
        // Workspace 页面
        {
          path: 'workspace/:name',
          name: 'WorkspacePage',
          component: () => import('../../views/workspace/WorkspacePage.vue')
        },
        // 页面类型 pagetype 列表/详情 - 支持多种格式
        {
          path: 'app/:entity',
          name: 'PageTypeList',
          component: () => import('../../core/pagetype/GenericListPage.vue')
        },
        {
          path: 'app/:entity/:id',
          name: 'PageTypeDetail',
          component: () => import('../../core/pagetype/GenericDetailPage.vue')
        },
        // 保持向后兼容
        {
          path: 'page/:entity',
          name: 'PageTypeListLegacy',
          component: () => import('../../core/pagetype/GenericListPage.vue')
        },
        {
          path: 'page/:entity/:id',
          name: 'PageTypeDetailLegacy',
          component: () => import('../../core/pagetype/GenericDetailPage.vue')
        },
        {
          path: 'settings/menu',
          name: 'MenuManager',
          component: () => import('../../views/settings/MenuManager.vue')
        },
        {
          path: 'settings',
          name: 'Settings',
          component: () => import('../../views/Settings.vue')
        },
        {
          path: 'search',
          name: 'SearchResults',
          component: () => import('../../views/SearchResults.vue')
        },
        // 开发
        {
          path: 'dev/create-pagetype-template',
          name: 'CreatePagetypeTemplate',
          component: () => import('../../views/dev/CreatePagetypeTemplate.vue')
        },
        {
          path: 'dev/create-app-template',
          name: 'CreateAppTemplate',
          component: () => import('../../views/dev/CreateAppTemplate.vue')
        },
        {
          path: 'app-installer',
          name: 'AppInstaller',
          component: () => import('../../views/dev/AppInstaller.vue')
        },
        {
          path: 'installed-apps',
          name: 'InstalledApps',
          component: () => import('../../views/dev/InstalledApps.vue')
        },
        {
          path: 'app-marketplace',
          name: 'AppMarketplace',
          component: () => import('../../views/dev/AppMarketplace.vue')
        },
        {
          path: 'app-marketplace/:name',
          name: 'AppDetail',
          component: () => import('../../views/dev/AppDetail.vue')
        },
        {
          path: 'publish-app',
          name: 'PublishApp',
          component: () => import('../../views/dev/PublishApp.vue'),
          meta: { requiresAuth: true }
        }
      ]
    }
  ]
})

// 路由守卫
router.beforeEach(async (to, _from, next) => {
  const authStore = useAuthStore()
  
  // 初始化认证状态
  if (!authStore.isAuthenticated) {
    await authStore.initAuth()
  }
  
  if (to.meta.requiresAuth && !authStore.isLoggedIn) {
    next('/login')
  } else if (to.path === '/login' && authStore.isLoggedIn) {
    next('/')
  } else {
    next()
  }
})

export default router
