import { createRouter, createWebHistory } from 'vue-router'
import { useUserStore } from '../stores/user'
import Home from '../views/Home.vue'
import Login from '../views/Login.vue'
import NotFound from '../views/NotFound.vue'
import AdminLayout from '../views/admin/AdminLayout.vue'
const TeacherList = () => import('../views/admin/teacher/TeacherList.vue')
const TeacherCreate = () => import('../views/admin/teacher/TeacherCreate.vue')
const TeacherEdit = () => import('../views/admin/teacher/TeacherEdit.vue')
const TeacherDetail = () => import('../views/admin/teacher/TeacherDetail.vue')

const routes = [


  {
    name: "main",
    path: '/practice',
    // component: Main,
    component: () => import('../views/oj/Main.vue'),
    meta: {
      title: "Home - Online Judge - XJ",
    }
  },


  {
    path: '/',
    name: 'Home',
    component: Home
  },
  {
    path: '/login',
    name: 'Login',
    component: Login
  },
  {
    path: '/course',
    name: 'CourseCenter',
    component: () => import('../views/course/CourseCenter.vue')
  },
  {
    path: '/course/detail/:id',
    name: 'CourseDetail',
    component: () => import('../views/course/CourseDetail.vue')
  },



  {
    path: '/exam',
    name: 'ExamCenter',
    // component: () => import('../views/exam/ExamCenter.vue')
    component: () => import('../views/paper/exam/list.vue')
  },


  {
    path: '/exam/prepare/:examId',
    component: () => import('../views/paper/exam/preview.vue'),
    name: 'PreExam',
    meta: { title: '准备考试', noCache: true, activeMenu: '/my/exam' },
    hidden: true
  },


  {
    path: '/exam/start/:id',
    component: () => import('../views/paper/exam/exam.vue'),
    name: 'StartExam',

    meta: {

      title: '开始考试',

      isExam: true,

      hideNav: true

    },
    hidden: true
  },


  {
    path: '/exam/:id',
    name: 'ExamDetail',
    component: () => import('../views/exam/ExamDetail.vue')
  },
  {
    path: '/practice',
    name: 'PracticeCenter',
    component: () => import('../views/practice/PracticeCenter.vue')
  },
  {
    path: '/activity',
    name: 'ActivityCenter',
    component: () => import('../views/activity/ActivityCenter.vue')
  },
  {
    path: '/test',
    name: 'TestCenter',
    component: () => import('../views/activity/TestCenter.vue')
  },
  {
    path: '/mall',
    name: 'Mall',
    component: () => import('../views/mall/Mall.vue')
  },
  {
    path: '/announcement',
    name: 'Announcement',
    component: () => import('../views/announcement/Announcement.vue')
  },
  {
    path: '/my',
    name: 'UserCenter',
    component: () => import('../views/user/UserCenter.vue'),
    children: [
      {
        path: 'courses',
        name: 'MyCourses',
        component: () => import('../views/user/MyCourses.vue')
      },
      {
        path: 'profile',
        name: 'UserProfile',
        component: () => import('../views/user/UserProfile.vue')
      }
    ]
  },
  {
    path: '/admin',
    name: 'AdminLayout',
    component: () => import('../views/admin/AdminLayout.vue'),
    meta: { requiresAuth: true, requiresAdmin: true },
    children: [
      {
        path: 'dashboard',
        name: 'AdminDashboard',
        component: () => import('../views/admin/Dashboard.vue')
      },
      {
        path: 'courses',
        name: 'AdminCourses',
        component: () => import('../views/admin/course/CourseList.vue')
      },
      {
        path: 'teachers',
        name: 'AdminTeachers',
        component: () => import('../views/admin/teacher/TeacherList.vue')
      },
      {
        path: 'exams',
        name: 'AdminExams',
        component: () => import('../views/admin/exam/ExamList.vue')
      }
    ]
  },
  {
    path: '/:pathMatch(.*)*',
    name: 'NotFound',
    component: NotFound
  }
]

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

// 路由守卫
router.beforeEach((to, from, next) => {
  const userStore = useUserStore()

  if (to.meta.requiresAuth && !userStore.isLoggedIn) {
    next('/login')
  } else if (to.meta.requiresAdmin && !userStore.isAdmin) {
    next('/')
  } else {
    next()
  }
})

export default router
