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

const routes = [
  { path: '/', name: 'home', component: () => import('./pages/Home.vue') },
  { path: '/blog', name: 'blog', component: () => import('./pages/Blog.vue') },
  { path: '/blog/:id', name: 'blog-detail', component: () => import('./pages/BlogDetail.vue') },
  { path: '/columns', name: 'columns', component: () => import('./pages/Columns.vue') },
  { path: '/columns/:id', name: 'column-detail', component: () => import('./pages/ColumnDetail.vue') },
  { path: '/works', name: 'works', component: () => import('./pages/Works.vue') },
  { path: '/works/:id', name: 'work-detail', component: () => import('./pages/WorkDetail.vue') },
  { path: '/snippets', name: 'snippets', component: () => import('./pages/Snippets.vue') },
  { path: '/services', name: 'services', component: () => import('./pages/Services.vue') },
  { path: '/about', name: 'about', component: () => import('./pages/About.vue') },
  // 登录路由
  { path: '/login', name: 'login', component: () => import('./pages/Login.vue') },
  
  // 管理员路由 - 使用 AdminLayout 组件
  {
    path: '/admin',
    name: 'admin',
    component: () => import('./components/admin/AdminLayout.vue'),
    meta: { requiresAuth: true },
    children: [
      // 仪表盘
      { path: '', redirect: '/admin/dashboard' },
      { path: 'dashboard', name: 'admin-dashboard', component: () => import('./pages/admin/dashboard/Overview.vue') },
      { path: 'analytics', name: 'admin-analytics', component: () => import('./pages/admin/dashboard/Analytics.vue') },
      
      // 用户管理
      { path: 'users', name: 'admin-users', component: () => import('./pages/admin/Users.vue') },
      { path: 'users/create', name: 'admin-users-create', component: () => import('./pages/admin/UserForm.vue') },
      { path: 'users/:id/edit', name: 'admin-users-edit', component: () => import('./pages/admin/UserForm.vue') },
      
      // 角色管理
      { path: 'roles', name: 'admin-roles', component: () => import('./pages/admin/Roles.vue') },
      { path: 'roles/create', name: 'admin-roles-create', component: () => import('./pages/admin/RoleForm.vue') },
      { path: 'roles/:id/edit', name: 'admin-roles-edit', component: () => import('./pages/admin/RoleForm.vue') },
      
      // 文章管理
      { path: 'posts', name: 'admin-posts', component: () => import('./pages/admin/Posts.vue') },
      { path: 'posts/create', name: 'admin-posts-create', component: () => import('./pages/admin/PostForm.vue') },
      { path: 'posts/:id/edit', name: 'admin-posts-edit', component: () => import('./pages/admin/PostForm.vue') },
      
      // 分类管理
      { path: 'categories', name: 'admin-categories', component: () => import('./pages/admin/Categories.vue') },
      { path: 'categories/create', name: 'admin-categories-create', component: () => import('./pages/admin/CategoryForm.vue') },
      { path: 'categories/:id/edit', name: 'admin-categories-edit', component: () => import('./pages/admin/CategoryForm.vue') },

      // 专栏管理
      { path: 'columns', name: 'admin-columns', component: () => import('./pages/admin/Columns.vue') },
      { path: 'columns/create', name: 'admin-columns-create', component: () => import('./pages/admin/ColumnForm.vue') },
      { path: 'columns/:id/edit', name: 'admin-columns-edit', component: () => import('./pages/admin/ColumnForm.vue') },

      // 作品管理
      { path: 'works', name: 'admin-works', component: () => import('./pages/admin/Works.vue') },
      { path: 'works/create', name: 'admin-works-create', component: () => import('./pages/admin/WorkForm.vue') },
      { path: 'works/:id/edit', name: 'admin-works-edit', component: () => import('./pages/admin/WorkForm.vue') },
      
      // 代码片段管理
      { path: 'snippets', name: 'admin-snippets', component: () => import('./pages/admin/Snippets.vue') },
      { path: 'snippets/create', name: 'admin-snippets-create', component: () => import('./pages/admin/SnippetForm.vue') },
      { path: 'snippets/:id/edit', name: 'admin-snippets-edit', component: () => import('./pages/admin/SnippetForm.vue') },
      
      // 标签管理
      { path: 'tags', name: 'admin-tags', component: () => import('./pages/admin/Tags.vue') },
      { path: 'tags/create', name: 'admin-tags-create', component: () => import('./pages/admin/TagForm.vue') },
      { path: 'tags/:id/edit', name: 'admin-tags-edit', component: () => import('./pages/admin/TagForm.vue') },
      
      // 关于页面管理
      { path: 'about', name: 'admin-about', component: () => import('./pages/admin/About.vue') },

      // 合作咨询管理
      { path: 'inquiries', name: 'admin-inquiries', component: () => import('./pages/admin/Inquiries.vue') },

      // 客户评价管理
      { path: 'testimonials', name: 'admin-testimonials', component: () => import('./pages/admin/Testimonials.vue') },
      { path: 'testimonials/create', name: 'admin-testimonials-create', component: () => import('./pages/admin/TestimonialForm.vue') },
      { path: 'testimonials/:id/edit', name: 'admin-testimonials-edit', component: () => import('./pages/admin/TestimonialForm.vue') },

      // 合作伙伴管理
      { path: 'partners', name: 'admin-partners', component: () => import('./pages/admin/Partners.vue') },
      { path: 'partners/create', name: 'admin-partners-create', component: () => import('./pages/admin/PartnerForm.vue') },
      { path: 'partners/:id/edit', name: 'admin-partners-edit', component: () => import('./pages/admin/PartnerForm.vue') },

      // 邮箱后缀配置
      { path: 'email-suffixes', name: 'admin-email-suffixes', component: () => import('./pages/admin/EmailSuffixes.vue') },

      // 系统配置
      { path: 'settings', name: 'admin-settings', component: () => import('./pages/admin/Settings.vue') },
      
      // 操作日志
      { path: 'logs', name: 'admin-logs', component: () => import('./pages/admin/Logs.vue') },
      
      // 附件管理
      { path: 'attachments', name: 'admin-attachments', component: () => import('./pages/admin/Attachments.vue') },
      
      // 附件分类管理
      { path: 'attachment-categories', name: 'admin-attachment-categories', component: () => import('./pages/admin/AttachmentCategories.vue') },
      { path: 'attachment-categories/create', name: 'admin-attachment-categories-create', component: () => import('./pages/admin/AttachmentCategoryForm.vue') },
      { path: 'attachment-categories/:id/edit', name: 'admin-attachment-categories-edit', component: () => import('./pages/admin/AttachmentCategoryForm.vue') },
      
      // OSS配置管理
      { path: 'oss-configs', name: 'admin-oss-configs', component: () => import('./pages/admin/OSSConfigs.vue') }
    ]
  }
]

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

// 路由守卫，用于保护需要认证的路由
router.beforeEach((to, _from, next) => {
  // 检查路由是否需要认证
  if (to.matched.some(record => record.meta.requiresAuth)) {
    // 检查本地存储中是否有token
    const token = localStorage.getItem('token')
    if (!token) {
      // 没有token，重定向到登录页
      next({ name: 'login' })
    } else {
      // 有token，继续访问
      next()
    }
  } else {
    // 不需要认证的路由，直接访问
    next()
  }
})

export default router