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

const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: [
    // 登录模块
    {
      path: '/login',
      component: () => import('@/views/login/LoginPage.vue')
    },
    // 密码重置
    {
      path: '/password/reset',
      component: () => import('@/views/password/PasswordReset.vue'),
      meta: { noAuth: true } // 添加不需要认证的标记
    },
    // 移动端模块 start
    {
      // 首页
      path: '/mobile/index',
      component: () => import('@/views/mobile/IndexPage.vue')
    },
    {
      // 场地信息
      path: '/mobile/detail',
      component: () => import('@/views/mobile/VenueDetail.vue')
    },
    {
      // 时间段信息
      path: '/mobile/time',
      component: () => import('@/views/mobile/VenueTime.vue')
    },
    {
      // 预约成功响应页面
      path: '/mobile/result/success',
      component: () => import('@/views/mobile/result/SuccessResult.vue')
    },
    {
      // 预约失败响应页面
      path: '/mobile/result/fail',
      component: () => import('@/views/mobile/result/FailureResult.vue')
    },
    {
      // 预约记录页面
      path: '/mobile/record',
      component: () => import('@/views/mobile/RecordPage.vue')
    },
    {
      // 个人信息页面
      path: '/mobile/userinfo',
      component: () => import('@/views/mobile/UserInfo.vue')
    },
    {
      // 聊天页面
      path: '/mobile/message',
      component: () => import('@/views/mobile/MessagePage.vue')
    },
    {
      // 提问页面
      path: '/mobile/question',
      component: () => import('@/views/mobile/QuestionPage.vue')
    },
    // 移动端模块 end
    // 超级管理员
    {
      path: '/',
      component: () => import('@/views/layout/LayoutContainer.vue'),
      redirect: '/index',
      children: [
        // 系统首页
        {
          path: '/index',
          component: () => import('@/views/index/indexPage.vue')
        },
        // 轮播事件管理
        {
          path: '/carousel',
          component: () => import('@/views/carousel/CarouselManage.vue')
        },
        // 场馆信息管理
        {
          path: '/type/manage',
          component: () => import('@/views/type/TypeManage.vue')
        },
        // 场地信息管理
        {
          path: '/field/manage',
          component: () => import('@/views/field/FieldManage.vue')
        },
        // 预约时间管理
        {
          path: '/time/manage',
          component: () => import('@/views/time/VenueTime.vue')
        },
        // 评论管理 -- 用户评论
        {
          path: '/comment/user',
          component: () => import('@/views/comment/CommentPage.vue')
        },
        // 评论管理 -- 敏感词
        {
          path: '/comment/word',
          component: () => import('@/views/word/WordPage.vue')
        },
        // 后台数据可视化
        {
          path: '/data/visible',
          component: () => import('@/views/data/DataVisualization.vue')
        },
        // 用户信息查看
        {
          path: '/user/manage',
          component: () => import('@/views/manage/UserManage.vue')
        },
        // 管理员信息查看
        {
          path: '/user/admin',
          component: () => import('@/views/manage/AdminManage.vue')
        },
        // 系统日志
        {
          path: '/system/log',
          component: () => import('@/views/log/SystemMessage.vue')
        },
        // 个人中心
        {
          path: '/user/info',
          component: () => import('@/views/user/UserPage.vue')
        },
        // 留言箱（基于websocket实现）
        {
          path: '/message/box',
          component: () => import('@/views/message/MessagePage.vue')
        },
        // 留言箱（基于netty实现）
        {
          path: '/message/netty',
          component: () => import('@/views/message/NettyPage.vue')
        },
        // ai问答
        {
          path: '/ai',
          component: () => import('@/views/ai/AnsweringPage.vue')
        }
      ]
    },
    // 默认重定向到登录页
    {
      path: '/:pathMatch(.*)*',
      redirect: '/login'
    }
  ]
})

// 登录拦截访问
router.beforeEach((to) => {
  const useStore = useUserStore()

  // 不需要认证的页面直接放行
  if (to.meta.noAuth) {
    return true
  }

  // 如果没有 token 且访问的是非登录页，拦截到登录
  if (!useStore.token && to.path !== '/login') {
    return '/login'
  }

  // 其他情况正常放行
  return true
})

export default router
