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

const MainLayout = () => import("@/views/MainLayout.vue");
const Dashboard = () => import("@/views/Dashboard.vue");
const Error404NotFound = () => import("@/views/Error404NotFound.vue");
const loginView = () => import("@/views/login.vue");
const ErrorPermission = () => import("@/views/ErrorPermission.vue");

import ModuleMetadataNew from "@/modules/01_masterdata/router";
import ModuleOrganizationalNew from "@/modules/02_organization/router";
import ModulePositionEmployee from "@/modules/16_positionEmployee/router";
import ModuleRecruiting from "@/modules/03_recruiting/router";
import ModuleRegistryNew from "@/modules/04_registryPerson/router";
import ModulePlacement from "@/modules/05_placement/router";
import ModuleRetirement from "@/modules/06_retirement/router";
import ModuleInsignia from "@/modules/07_insignia/router";
import ModuleRegistryEmployee from "@/modules/08_registryEmployee/router";
import ModuleLeave from "@/modules/09_leave/router";
import ModuleDiscipline from "@/modules/11_discipline/router";
import ModuleEvaluate from "@/modules/12_evaluatePersonal/router";
import ModuleSalary from "@/modules/13_salary/router";
import ModuleKPI from "@/modules/14_KPI/router";
import ModuleDevelopment from "@/modules/15_development/router";
import ModuleSupport from "@/modules/00_support/router";
import ModuleActing from "@/modules/17_acting/router";
import ModuleCommand from "@/modules/18_command/router";
import ModulePositionCondition from "@/modules/19_condition/router";

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

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: "HOME",
          },
        },
        {
          path: "/error",
          name: "errorPermission",
          component: ErrorPermission,
        },
        // ...ModuleMetadata,
        ...ModuleMetadataNew,
        // ...ModuleOrganizational,
        ...ModuleOrganizationalNew,
        ...ModulePositionEmployee,
        ...ModuleRecruiting,
        // ...ModuleRegistry,
        ...ModuleRegistryNew,
        ...ModulePlacement,
        ...ModuleRetirement,
        ...ModuleInsignia,
        ...ModuleRegistryEmployee,
        ...ModuleLeave,
        ...ModuleDiscipline,
        ...ModuleEvaluate,
        ...ModuleSalary,
        ...ModuleKPI,
        ...ModuleDevelopment,
        ...ModuleSupport,
        ...ModuleActing,
        ...ModuleCommand,
        ...ModulePositionCondition,
      ],
    },
    /**
     * 404 Not Found
     * ref: https://router.vuejs.org/guide/essentials/dynamic-matching.html#catch-all-404-not-found-route
     */
    {
      // path: "/:catchAll(.*)*", // TODO: ใช้ pathMatch แทนตามในเอกสารแนะนำ คงไว้เผื่อจำเป็น
      path: "/:pathMatch(.*)*",
      component: Error404NotFound,
    },

    // authen with client
    {
      path: "/login",
      name: "loginMain",
      component: loginView,
      meta: {
        Auth: false,
      },
    },
    {
      path: "/auth",
      name: "auth",
      component: () => import("@/views/auth.vue"),
    },
  ],

  scrollBehavior(to, from, savedPosition) {
    if (savedPosition) {
      return savedPosition;
    } else if (to.hash) {
      return {
        el: to.hash,
        behavior: "smooth",
      };
    }
  },
});

// authen with client
router.beforeEach(async (to, from, next) => {
  if (to.meta.Auth) {
    const checkAuthen = await authenticated();
    if (!checkAuthen && to.meta.Auth) {
      router.push({ name: "loginMain" });
    }
  }
  next();
});

export default router;
