import DashboardPage from "@/pages/DashboardPage.vue";
import LoginPage from "@/pages/LoginPage.vue";
import {
  handleCourseSessions,
  redirectToLoginIfRequired,
  updateLoggedIn,
} from "@/router/guards";
import { createRouter, createWebHistory } from "vue-router";

const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  scrollBehavior(to, from, savedPosition) {
    if (savedPosition) {
      return savedPosition;
    } else {
      return { top: 0 };
    }
  },
  routes: [
    {
      path: "/login",
      component: LoginPage,
      props: { loginMethod: "sso" },
      meta: {
        // no login required -> so `public === true`
        public: true,
      },
    },
    {
      path: "/login-local",
      component: LoginPage,
      props: { loginMethod: "local" },
      meta: {
        // no login required -> so `public === true`
        public: true,
      },
    },
    {
      path: "/",
      name: "home",
      component: DashboardPage,
    },
    {
      path: "/course/:courseSlug/media",
      props: true,
      component: () => import("@/pages/mediaLibrary/MLParentPage.vue"),
      children: [
        {
          path: "",
          component: () => import("@/pages/mediaLibrary/MLIndexPage.vue"),
        },
        {
          path: "category/:mediaCategorySlug/media",
          props: true,
          component: () => import("@/pages/mediaLibrary/MLMediaListPage.vue"),
        },
        {
          path: "category/:mediaCategorySlug",
          props: true,
          component: () => import("@/pages/mediaLibrary/MLCategoryDetailPage.vue"),
        },
        {
          path: "category",
          component: () => import("@/pages/mediaLibrary/MLCategoryIndexPage.vue"),
        },
      ],
    },
    {
      path: "/course/:courseSlug/competence",
      props: true,
      component: () => import("@/pages/competence/CompetenceParentPage.vue"),
      children: [
        {
          path: "",
          props: true,
          component: () => import("@/pages/competence/CompetenceIndexPage.vue"),
        },
        {
          path: "competences",
          props: true,
          component: () => import("@/pages/competence/CompetenceListPage.vue"),
        },
        {
          path: "criteria",
          props: true,
          component: () => import("@/pages/competence/PerformanceCriteriaPage.vue"),
        },
        {
          path: "criteria/:criteriaSlug",
          props: true,
          component: () =>
            import("@/pages/competence/SinglePerformanceCriteriaPage.vue"),
        },
      ],
    },
    {
      path: "/course/:courseSlug/learn",
      component: () =>
        import("../pages/learningPath/learningPathPage/LearningPathPage.vue"),
      props: true,
    },
    {
      path: "/course/:courseSlug/learn/:circleSlug",
      component: () => import("../pages/learningPath/circlePage/CirclePage.vue"),
      props: true,
    },
    {
      path: "/course/:courseSlug/learn/:circleSlug/evaluate/:learningUnitSlug",
      component: () =>
        import("../pages/learningPath/selfEvaluationPage/SelfEvaluationPage.vue"),
      props: true,
    },
    {
      path: "/course/:courseSlug/learn/:circleSlug/:contentSlug",
      component: () =>
        import("../pages/learningPath/learningContentPage/LearningContentPage.vue"),
      props: true,
    },
    {
      path: "/course/:courseSlug/cockpit",
      props: true,
      component: () => import("@/pages/cockpit/CockpitParentPage.vue"),
      children: [
        {
          path: "",
          component: () => import("@/pages/cockpit/cockpitPage/CockpitPage.vue"),
          props: true,
        },
        {
          path: "profile/:userId",
          component: () => import("@/pages/cockpit/CockpitUserProfilePage.vue"),
          props: true,
        },
        {
          path: "profile/:userId/:circleSlug",
          component: () => import("@/pages/cockpit/CockpitUserCirclePage.vue"),
          props: true,
        },
        {
          path: "feedback/:circleId",
          component: () => import("@/pages/cockpit/FeedbackPage.vue"),
          props: true,
        },
        {
          path: "assignment",
          component: () =>
            import("@/pages/cockpit/assignmentsPage/AssignmentsPage.vue"),
          props: true,
        },
        {
          path: "assignment/:assignmentId/:userId",
          component: () =>
            import(
              "@/pages/cockpit/assignmentEvaluationPage/AssignmentEvaluationPage.vue"
            ),
          props: true,
        },
      ],
    },
    {
      path: "/shop",
      component: () => import("@/pages/ShopPage.vue"),
    },
    {
      path: "/messages",
      component: () => import("@/pages/MessagesPage.vue"),
    },
    {
      path: "/settings",
      component: () => import("@/pages/SettingsPage.vue"),
    },
    {
      path: "/notifications",
      component: () => import("@/pages/NotificationsPage.vue"),
    },
    {
      path: "/styleguide",
      component: () => import("../pages/StyleGuidePage.vue"),
      meta: {
        public: true,
      },
    },
    {
      path: "/:pathMatch(.*)*",
      component: () => import("../pages/404Page.vue"),
    },
  ],
});

router.beforeEach(updateLoggedIn);
router.beforeEach(redirectToLoginIfRequired);

// register after login hooks
router.beforeEach(handleCourseSessions);

export default router;
