// The Lucia project.
// Copyright 2023-2026 DSP, inc.  All rights reserved.
// Authors:
//   chiayin.kuo@dsp.im (chiayin), 2023/1/31
//   imacat.yang@dsp.im (imacat), 2023/9/23
//   cindy.chang@dsp.im (Cindy Chang), 2024/5/30
/**
 * @module router Vue Router configuration with route definitions,
 *   navigation guards, and authentication redirect logic.
 */

import { createRouter, createWebHistory } from "vue-router";
import { useLoginStore } from "@/stores/login";
import { getCookie, setCookie } from "@/utils/cookieUtil";
import { encodeReturnTo } from "@/utils/returnToEncoding";
import { evaluateAuthNavigation } from "@/router/authGuard";
import AuthContainer from "@/views/AuthContainer.vue";
import MainContainer from "@/views/MainContainer.vue";
import Login from "@/views/Login/LoginPage.vue";
import Files from "@/views/Files/FilesPage.vue";
import Upload from "@/views/Upload/UploadPage.vue";
import Map from "@/views/Discover/Map/MapPage.vue";
import Conformance from "@/views/Discover/Conformance/ConformancePage.vue";
import Performance from "@/views/Discover/Performance/PerformancePage.vue";
import CompareDashboard from "@/views/Compare/Dashboard/CompareDashboard.vue";
import MapCompare from "@/views/Compare/MapCompare.vue";
import AccountAdmin from "@/views/AccountManagement/AccountAdmin/AccountAdmin.vue";
import MyAccount from "@/views/AccountManagement/MyAccount.vue";
import MemberArea from "@/views/MemberArea/MemberAreaPage.vue";
import NotFound404 from "@/views/NotFound404.vue";

const routes = [
  {
    path: "/", // Default entry route
    redirect: "/files", // Redirect to /files
  },
  {
    path: "/",
    name: "AuthContainer",
    component: AuthContainer,
    children: [
      {
        path: "/login",
        name: "Login",
        component: Login,
      },
    ],
  },
  {
    path: "/",
    name: "MainContainer",
    component: MainContainer,
    meta: {
      title: "MainContainer",
      requiresAuth: true,
    },
    children: [
      {
        path: "member-area",
        name: "MemberArea",
        component: MemberArea,
      },
      {
        path: "files",
        name: "Files",
        component: Files,
      },
      {
        path: "",
        name: "Account Management",
        children: [
          {
            path: "account-admin",
            name: "AcctAdmin",
            component: AccountAdmin,
          },
        ],
      },
      {
        path: "my-account",
        name: "My Account",
        component: MyAccount,
      },
      {
        path: "upload", // router.push({ replace: true }) does not add the path to history
        name: "Upload",
        component: Upload,
      },
      {
        path: "discover",
        name: "Discover",
        children: [
          {
            // type: log | filter, the parameter can be either.
            // fileId: log_id | filter_id, the parameter can be either.
            path: ":type/:fileId/map",
            name: "Map",
            component: Map,
          },
          {
            // type: log | filter, the parameter can be either.
            // fileId: log_id | filter_id, the parameter can be either.
            path: ":type/:fileId/conformance",
            name: "Conformance",
            component: Conformance,
          },
          {
            // type: log | filter, the parameter can be either.
            // fileId: log_id | filter_id, the parameter can be either.
            path: ":type/:fileId/performance",
            name: "Performance",
            component: Performance,
          },
          {
            // type: log | filter, the parameter can be either.
            // fileId: check_id, fetches parent data via `/log-checks/{check_id}`
            path: "conformance/:type/:fileId/map",
            name: "CheckMap",
            component: Map,
            meta: {
              file: {}, // parent log or parent filter
            },
          },
          {
            // type: log | filter, the parameter can be either.
            // fileId: check_id, fetches parent data via `/log-checks/{check_id}`
            path: "conformance/:type/:fileId/conformance",
            name: "CheckConformance",
            component: Conformance,
            meta: {
              file: {}, // parent log or parent filter
            },
          },
          {
            // type: log | filter, the parameter can be either.
            // fileId: check_id, fetches parent data via `/log-checks/{check_id}`
            path: "conformance/:type/:fileId/performance",
            name: "CheckPerformance",
            component: Performance,
            meta: {
              file: {}, // parent log or parent filter
            },
          },
        ],
      },
      {
        path: "compare",
        name: "Compare",
        children: [
          {
            path: "dashboard/:primaryType/:primaryId/:secondaryType/:secondaryId",
            name: "CompareDashboard",
            component: CompareDashboard,
          },
          {
            path: "map/:primaryType/:primaryId/:secondaryType/:secondaryId",
            name: "MapCompare",
            component: MapCompare,
          },
        ],
      },
    ],
  },
  {
    path: "/:pathMatch(.*)*",
    name: "NotFound404",
    component: NotFound404,
  },
];

const base_url = import.meta.env.BASE_URL;
const router = createRouter({
  history: createWebHistory(base_url), //(/)
  // history: createWebHashHistory(base_url), // (/#)
  routes,
});

// Global navigation guard
router.beforeEach(async (to) => {
  return evaluateAuthNavigation(to, {
    getCookie,
    refreshSession: async () => {
      const loginStore = useLoginStore();
      await loginStore.refreshToken();
      loginStore.setIsLoggedIn(true);
    },
    setLoginMarker: () => setCookie("isLuciaLoggedIn", "true"),
    encodeReturnTo,
  });
});

export default router;
