import { useAuthStore } from "@/stores/auth.store";
import type { SearchRouteParam } from "@/types/route.type";
import { createRouter, createWebHistory } from "vue-router";
import RouteView from "../views/RouteView.vue";

export const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: [
    {
      path: "/",
      name: "Home",
      redirect: "/report/index"
    },
    {
      path: "/report",
      name: "Report",
      component: RouteView,
      children: [
        {
          path: "index",
          name: "ReportList",
          component: () => import("../views/report/ReportListView.vue"),
        },
        {
          path: "edit/:id",
          name: "ReportEditor",
          component: () => import("../views/report/ReportEditorView.vue"),
        },
        {
          path: "preview/:id",
          name: "ReportPreview",
          component: () => import("../views/reportPreview/ReportPreviewView.vue"),
        },
        {
          path: "template",
          name: "ReportTemplate",
          component: () => import("../views/reportTemplate/ReportTemplateListView.vue"),
        },
        {
          path: "template/edit/:id",
          name: "ReportTemplateEditor",
          component: () => import("../views/reportTemplate/ReportTemplateEditorView.vue"),
        },
      ]
    },
    {
      path: "/search",
      name: "Search",
      component: RouteView,
      children: [
        {
          path: "index",
          name: "SearchIndex",
          component: () => import("../views/search/SearchView.vue")
        },
        {
          path: "advanced/index",
          name: "AdvancedSearchIndex",
          component: () => import("../views/search/AdvancedSearchView.vue"),
        },
        {
          path: "result",
          name: "SearchResult",
          component: () => import("../views/search/SearchResultView.vue")
        }
      ]
    },
    {
      path: "/detail",
      name: "DetailRoot",
      component: RouteView,
      children: [
        {
          path: ":id",
          name: "Detail",
          component: () => import("../views/detail/DetailView.vue"),
        }
      ]
    },
    {
      path: "/knowledgeGraph",
      name: "KnowledgeGraph",
      component: RouteView,
      children: [
        {
          path: "search",
          name: "KnowledgeGraph",
          component: () => import("../views/knowledgeGraph/KnowledgeGraphView.vue")
        }
      ]
    },
    {
      path: "/login",
      name: "Login",
      // route level code-splitting
      // this generates a separate chunk (About.[hash].js) for this route
      // which is lazy-loaded when the route is visited.
      component: () => import("../views/LoginView.vue"),
    },
    {
      path: "/history",
      name: "History",
      component: RouteView,
      children: [
        {
          path: "",
          name: "HistoryIndex",
          component: () => import("../views/history/HistoryView.vue"),
        }
      ]
    },
    {
      path: "/favorite",
      name: "Favorite",
      component: RouteView,
      children: [
        {
          path: "",
          name: "FavoriteIndex",
          component: () => import("../views/favorite/FavoriteView.vue"),
        }
      ]
    },
    {
      path: '/:pathMatch(.*)*',
      name: 'NotFound',
      component: () => import("../views/NotFound.vue"),
    },
  ],
});

router.beforeResolve(async to => {
  const authStore = useAuthStore();

  if (to.name != 'Login' && !authStore.isLoginIn()) {
    return { name: "Login" };
  }
});

export function routeToHome() {
  router.push({ name: "Home" })
}

export function routeToLogin() {
  router.push({ name: "Login" });
}

export function routeToSearchPage() {
  router.push({ name: "SearchIndex"})
}

/**
 * 路由到报告编辑界面
 * @param id 报告ID
 */
export function routeToReportEditor(id: number) {
  router.push({ name: "ReportEditor", params: { id }});
}

/**
 * 路由到搜索结果页面，把查询表达式传入到搜索结果页面
 * @param param queryString参数
 */
export function routeToSearch(param: SearchRouteParam, extras: {[index: string]: any} = {}) {
  const queryStrParam: {[index: string]: any} = { ...param, ...extras };

  router.push({ name: 'SearchResult', query: queryStrParam });
}

/**
 * 路由到报告预览界面
 * @param id 报告ID
 */
export function routeToReportPreview(id: number) {
  router.push({ name: 'ReportPreview', params: { id } })
}
