// router/index.js
import { createRouter, createWebHistory } from 'vue-router'
import Login from '../views/Login.vue'
import WebPage from '../components/WebPage.vue'
import NotFound from '../views/NotFound.vue'
import { useUserStore } from '../store/user'
import { useAuthStore as usePostulanteStore } from '../store/postulanteStore'

const routes = [

  { path: '/', component: WebPage },

  { path: '/account/auth/login', component: Login, meta: { guest: true } },
  
  {
    path: '/login-postulante',
    name: 'login-postulante',
    component: () => import('../views/postulante/LoginView.vue'),
    meta: { guest: true },
  },

    {
    path: '/:pathMatch(.*)*',
    name: 'NotFound',
    component: NotFound
  },
  
  {
    path: '/resultados',
    name: 'Resultados',
    component: () => import('../components/WebPageSections/navbarcontent/Resultados.vue')
  },
    {
    path: '/modalidades/cepreuna',
    name: 'cepreuna',
    component: () => import('../components/WebPageSections/navbarcontent/Cepreuna.vue')
  },

      {
    path: '/modalidades/extraordinario',
    name: 'extraordinario',
    component: () => import('../components/WebPageSections/navbarcontent/Extraordinario.vue')
  },
    {
    path: '/modalidades/general',
    name: 'general',
    component: () => import('../components/WebPageSections/navbarcontent/General.vue')
  },



  {
    path: '/portal-postulante',
    name: 'portal-postulante',
    component: () => import('../views/postulante/PortalView.vue'),
    meta: { requiresAuth: true },
    children: [
      {
        path: '',
        name: 'DashboardPostulante',
        component: () => import('../views/postulante/Dashboard.vue'),
        meta: { requiresAuth: true}
      },
            {
        path: '/portal-postulante/test',
        name: 'TestPostulante',
        component: () => import('../views/postulante/Test.vue'),
        meta: { requiresAuth: true}
      },
      {
        path: '/portal-postulante/examen/:examenId',
        name: 'PanelExamen',
        component: () => import('../views/postulante/PreguntasExamen.vue'),
        meta: { requiresAuth: true }
      },
      {
        path: '/portal-postulante/resultados/:examenId',
        name: 'PanelResultados',
        component: () => import('../views/postulante/Resultados.vue'),
        meta: { requiresAuth: true }
      },

      {
        path: '/portal-postulante/pagos',
        name: 'PanelPagos',
        component: () => import('../views/postulante/Pagos.vue'),
        meta: { requiresAuth: true }
      },
        {
        path: '/portal-postulante/mis-procesos',
        name: 'PanelProcesos',
        component: () => import('../views/postulante/MisProcesos.vue'),
        meta: { requiresAuth: true }
      },
    ]

    
  },


  {
    path: '/usuario/dashboard',
    name: 'dashboard',
    component: () => import('../views/usuario/Dashboard.vue'),
    meta: { requiresAuth: true, role: 'usuario' }
  },


  {
    path: '/admin/dashboard',
    component: () => import('../views/administrador/layout/Layout.vue'),
    meta: { requiresAuth: true, role: 'administrador' },
    children: [
      {
        path: '/admin/dashboard',
        name: 'Dashboard',
        component: () => import('../views/administrador/Dashboard.vue'),
        meta: { requiresAuth: true, role: 'administrador' }
      },
      {
        path: '/admin/dashboard/areas',
        name: 'Areas',
        component: () => import('../views/administrador/areas/AreasList.vue'),
        meta: { requiresAuth: true, role: 'administrador' }
      },
      {
        path: '/admin/dashboard/cursos',
        name: 'Cursos',
        component: () => import('../views/administrador/cursos/CursosList.vue'),
        meta: { requiresAuth: true, role: 'administrador' }
      },
      {
        path: '/admin/dashboard/cursos/:id/preguntas',
        name: 'CursoPreguntas',
        component: () => import('../views/administrador/cursos/PreguntasCursoView.vue'),
        meta: { requiresAuth: true, role: 'administrador' }
      },
      {
        path: '/admin/dashboard/procesos',
        name: 'Procesos',
        component: () => import('../views/administrador/Procesos/ProcesosList.vue'),
        meta: { requiresAuth: true, role: 'administrador' }
      },
      {
        path: '/admin/dashboard/reglas',
        name: 'Reglas',
        component: () => import('../views/administrador/Procesos/ReglasList.vue'),
        meta: { requiresAuth: true, role: 'administrador' }
      },

      {
        path: '/admin/dashboard/procesos-admision',
        name: 'ProcesosAdmisionList',
        component: () => import('../views/administrador/procesoadmision/ProcesosAdmisionList.vue')
      },
      {
        path: '/admin/dashboard/procesos/:id/detalles',
        name: 'ProcesoAdmisionDetalles',
        component: () => import('../views/administrador/procesoadmision/ProcesoAdmisionDetalles.vue')
      },
            {
        path: '/admin/dashboard/lista-calificacion',
        name: 'CalificacionList',
        component: () => import('../views/administrador/Procesos/CalificacionTest.vue')
      },

      {
        path: '/admin/dashboard/lista-postulantes',
        name: 'PostulantesList',
        component: () => import('../views/administrador/estudiantes/ListPostulantes.vue')
      },

      {
        path: '/admin/dashboard/noticias',
        name: 'NoticiasAdmisionList',
        component: () => import('../views/administrador/procesoadmision/NoticiasAdmin.vue')
      }

    ]
  },


  {
    path: '/superadmin/dashboard',
    name: 'superadmin-dashboard',
    component: () => import('../views/superadmin/Dashboard.vue'),
    meta: { requiresAuth: true, role: 'superadmin' }
  },


  { path: '/unauthorized', name: 'Unauthorized', component: () => import('../views/403.vue') },
  { path: '/403', name: 'forbidden', component: () => import('../views/403.vue') }
]

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

router.beforeEach((to, from, next) => {
  const userStore = useUserStore()
  const postulanteStore = usePostulanteStore()

  if (to.meta.requiresAuth && !to.path.startsWith('/portal-postulante') && !userStore.isAuth) {
    return next('/login')
  }


  if (to.meta.requiresAuth && to.path.startsWith('/portal-postulante') && !postulanteStore.isAuthenticated) {
    return next('/login-postulante')
  }


  if (to.meta.guest && !to.path.startsWith('/login-postulante') && userStore.isAuth) {
    userStore.redirectByRole()
    return
  }

  if (to.meta.guest && to.path === '/login-postulante' && postulanteStore.isAuthenticated) {
    return next('/portal-postulante')
  }

  if (to.meta.role && !userStore.hasRole(to.meta.role)) {
    return next('/403')
  }

  next()
})

export default router
