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

const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: [
    {
      path: '/login',
      name: 'login',
      component: () => import('@/views/auth/LoginView.vue'),
      meta: { public: true },
    },
    {
      path: '/auth/callback',
      name: 'auth-callback',
      component: () => import('@/views/auth/AuthCallbackView.vue'),
      meta: { public: true },
    },

    // Student
    {
      path: '/',
      name: 'dashboard',
      component: () => import('@/views/student/DashboardView.vue'),
      meta: { role: 'student' },
    },
    {
      path: '/catalog',
      name: 'catalog',
      component: () => import('@/views/student/CatalogView.vue'),
      meta: { role: 'student' },
    },
    {
      path: '/lecture/:id',
      name: 'lecture-detail',
      component: () => import('@/views/student/LectureDetailView.vue'),
      meta: { role: 'student' },
    },
    {
      path: '/my-lectures',
      name: 'my-lectures',
      component: () => import('@/views/student/MyLecturesView.vue'),
      meta: { role: 'student' },
    },
    {
      path: '/review/:id',
      name: 'review-form',
      component: () => import('@/views/student/ReviewFormView.vue'),
      meta: { role: 'student' },
    },
    {
      path: '/profile',
      name: 'profile',
      component: () => import('@/views/student/ProfileView.vue'),
    },
    {
      path: '/notifications',
      name: 'notifications',
      component: () => import('@/views/student/NotificationsView.vue'),
    },

    // Teacher
    {
      path: '/teacher',
      name: 'teacher-dashboard',
      component: () => import('@/views/teacher/TeacherDashboardView.vue'),
      meta: { role: 'teacher' },
    },
    {
      path: '/teacher/lectures',
      name: 'teacher-lectures',
      component: () => import('@/views/teacher/TeacherLecturesView.vue'),
      meta: { role: 'teacher' },
    },
    {
      path: '/teacher/analytics',
      name: 'teacher-analytics',
      component: () => import('@/views/teacher/TeacherAnalyticsView.vue'),
      meta: { role: 'teacher' },
    },

    // Admin
    {
      path: '/admin',
      name: 'admin-dashboard',
      component: () => import('@/views/admin/AdminDashboardView.vue'),
      meta: { role: 'admin' },
    },
    {
      path: '/admin/users',
      name: 'admin-users',
      component: () => import('@/views/admin/AdminUsersView.vue'),
      meta: { role: 'admin' },
    },
    {
      path: '/admin/lectures',
      name: 'admin-lectures',
      component: () => import('@/views/admin/AdminLecturesView.vue'),
      meta: { role: 'admin' },
    },
    { path: '/admin/llm-queue', redirect: '/admin/reviews' },
    {
      path: '/admin/reviews',
      name: 'admin-reviews',
      component: () => import('@/views/admin/AdminReviewsView.vue'),
      meta: { role: 'admin' },
    },

    { path: '/:pathMatch(.*)*', redirect: '/' },
  ],
})

router.beforeEach(async (to) => {
  const auth = useAuthStore()
  const resolveDefaultRoute = () => {
    if (auth.user?.activeRole === 'teacher') return '/teacher'
    if (auth.user?.activeRole === 'admin') return '/admin'
    return '/'
  }
  if (!auth.initialized && !to.meta.public) {
    await auth.initialize()
  }
  if (!to.meta.public && !auth.isAuthenticated) {
    return '/login'
  }
  if (
    to.meta.role &&
    auth.user &&
    !auth.user.roles.includes(to.meta.role as 'student' | 'teacher' | 'admin')
  ) {
    return resolveDefaultRoute()
  }
})

export default router
