import { createRouter, createWebHistory } from "vue-router";

const MainLayout = () => import("@/views/MainLayout.vue");
const Dashboard = () => import("@/modules/01_dashboard/views/Dashboard.vue");
const Error404NotFound = () => import("@/views/Error404NotFound.vue");
const loginMain = () => import("@/views/login.vue");
const ErrorPermission = () => import("@/views/ErrorPermission.vue");
const VerifyEmail = () => import("@/views/VerifyEmail.vue");

import ModuleTransfer from "@/modules/02_transfer/router";
import ModuleRetire from "@/modules/03_retire/router";
import ModuleLeave from "@/modules/05_leave/router";
import ModuEvaluate from "@/modules/06_evaluate/router";
import ModuAppealComplain from "@/modules/07_appealComplain/router";
import ModuleSupport from "@/modules/00_support/router";
import ModuleKPI from "@/modules/08_KPI/router";
import ModuleScholarship from "@/modules/09_scholarship/router";
import ModuleRegistry from "@/modules/10_registry/router";
import ModuleProbation from "@/modules/11_probation/router";
import ModuleOrganization from "@/modules/12_organization/router";
import ModulePortfolio from "@/modules/13_portfolio/router";
import ModuleDevelop from "@/modules/14_IDP/router";
import ModuleProbationReport from "@/modules/15_probationReport/router";

import { authenticated, logout } from "@/plugins/auth";
// TODO: ใช้หรือไม่?

const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: [
    {
      path: "/",
      name: "home",
      component: MainLayout,
      children: [
        {
          path: "/",
          name: "dashboard",
          component: Dashboard,
          meta: {
            Auth: true,
            Key: [7],
          },
        },
        {
          path: "/error",
          name: "errorPermission",
          component: ErrorPermission,
        },

        ...ModuleTransfer,
        ...ModuleRetire,
        ...ModuleLeave,
        ...ModuEvaluate,
        ...ModuAppealComplain,
        ...ModuleSupport,
        ...ModuleKPI,
        ...ModuleScholarship,
        ...ModuleRegistry,
        ...ModuleProbation,
        ...ModuleOrganization,
        ...ModulePortfolio,
        ...ModuleDevelop,
        ...ModuleProbationReport,
      ],
    },
    {
      // path: "/:catchAll(.*)*", // TODO: ใช้ pathMatch แทนตามในเอกสารแนะนำ คงไว้เผื่อจำเป็น
      path: "/:pathMatch(.*)*",
      component: Error404NotFound,
    },
    {
      path: "/login",
      name: "loginMain",
      component: loginMain,
      meta: {
        Auth: false,
      },
    },
    {
      path: "/verifyemail",
      name: "verifyemail",
      component: VerifyEmail,
    },
    {
      path: "/auth",
      name: "auth",
      component: () => import("@/views/auth.vue"),
    },
  ],
});

// authen with client
router.beforeEach(async (to, from, next) => {
  if (to.meta.Auth) {
    const checkAuthen = await authenticated();
    if (!checkAuthen && to.meta.Auth) {
      logout(true);
      return;
    }
  }
  next();
});

export default router;
