import { createRouter, createWebHistory, isNavigationFailure } from "vue-router";
import Login from "@/views/Login/Login.vue";
import AppCopy from "@/views/AppCopy.vue";
import HomeView from "@/views/HomeView.vue";
import Layout from "@/views/KMPlatform/Layout.vue";
import Home from "@/views/KMPlatform/Home/Home.vue";
import KGBuilder from "@/views/KMPlatform/KGBuilder/KGBuilder.vue";
import KnowledgeBase from "@/views/KMPlatform/KnowledgeBase/KnowledgeBase.vue";
import OpenPlatform from "@/views/KMPlatform/OpenPlatform/OpenPlatform.vue";
import TerminologyManagement from "@/views/KMPlatform/KGBuilder/TM/TerminologyManagement.vue";
import EntityTypeManagement from "@/views/KMPlatform/KGBuilder/ETM/EntityTypeManagement.vue";
import KnowledgeBaseManagement from "@/views/KMPlatform/KnowledgeBase/KBM/KnowledgeBaseManagement.vue";
import KnowledgeManagement from "@/views/KMPlatform/KnowledgeBase/KM/KnowledgeManagement.vue";
import AccountManage from "@/views/KMPlatform/Permission/AccountManage.vue";
import PermissionManage from "@/views/KMPlatform/Permission/permission.vue";
import RoleManage from "@/views/KMPlatform/Permission/RoleManage.vue";
import EntityRelationshipTypeManagement from '@/views/KMPlatform/KGBuilder/KRTM/EntityRelationshipTypeManagement.vue'
import { getSessionVar, deleteSessionVar } from "@/utils/session";

const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: [
    {
      path: "/",
      component: AppCopy,
      redirect: "/home",
      children: [
        {
          path: "home",
          name: "home",
          component: HomeView,
        },
        {
          path: "workspace",
          name: "workspace",
          component: () => import("@/views/WorkspaceView.vue"),
          children: [
            {
              path: "queue/:id",
              name: "queue",
              component: () => import("@/views/QueueView.vue"),
            },
            {
              path: "worker",
              name: "worker",
              component: () => import("@/views/WorkerView.vue"),
            },
            {
              path: "graph",
              name: "graph",
              component: () => import("../views/GraphView.vue"),
            },
            {
              path: "graph-mgr/:id",
              name: "graph-mgr",
              component: () => import("../views/GraphManagement.vue"),
            },
          ],
        },
        {
          path: "about",
          name: "about",
          component: () => import("../views/AboutView.vue"),
        },
      ],
    },
    {
      path: "/login",
      name: "login",
      component: Login,
      meta: {

      }
    },

    {
      path: "/kmplatform",
      name: "kmplatform",
      component: Layout,
      redirect: "/kmplatform/home",
      children: [
        {
          path: "home",
          name: "kmplatform-home",
          meta: {
            title: "主页",
          },
          component: Home,
          children: [],
        },
        {
          path: "knowledgebase",
          name: "knowledgebase",
          meta: {
            title: "知识库",
            keepAlive: true,
          },
          component: KnowledgeBase,
          redirect: "/kmplatform/knowledgebase/kbm",
          children: [
            {
              path: "kbm",
              name: "kbm",
              meta: {
                title: "知识库",
                keepAlive: true,
              },
              component: KnowledgeBaseManagement,
            },
            {
              path: "km/:kbId",
              name: "km",
              meta: {
                title: "知识库",
                keepAlive: false,
              },
              component: KnowledgeManagement,
            },
          ],
        },
        {
          path: "kgbuilder",
          name: "kgbuilder",
          meta: {
            title: "知识图谱构建",
          },
          redirect: "/kmplatform/kgbuilder/tm",
          component: KGBuilder,
          children: [
            {
              path: "tm",
              name: "kgb-tm",
              meta: {
                title: "术语管理",
              },
              component: TerminologyManagement,
            },
            {
              path: 'etm',
              name: 'kgb-etm',
              meta: {
                title: "实体类型管理",
              },
              component: EntityTypeManagement,
            },
            {
              path: 'krtm',
              name: 'kgb-krtm',
              meta: {
                title: "实体关系类型管理"
              },
              component: EntityRelationshipTypeManagement,
            }
          ]
        },
        {
          path: "openplatform",
          name: "openplatform",
          meta: {
            title: "开放平台",
          },
          component: OpenPlatform,
          children: [],
        },
        {
          path: "kgpermission",
          name: "kgpermission",
          meta: {
            title: "系统权限",
          },
          redirect: "/kmplatform/kgpermission/AccountManage",
          component: PermissionManage,
          children: [
            {
              path: "RoleManage",
              name: "kgb-RoleManage",
              meta: {
                title: "角色管理",
              },
              component: RoleManage,
            },
            {
              path: "AccountManage",
              name: "kgb-AccountManage",
              meta: {
                title: "账号管理",
              },
              component: AccountManage,
            },
          ],
        },
      ],
    },
  ],
});

router.beforeEach((to, from, next) => {
  if (
    getSessionVar("session_id") == null &&
    getSessionVar("username") == null
  ) {
    if (to.name !== "login") {
      next({ name: "login" }); // 重定向到登录页面
    } else {
      next(); // 确认转移
    }
  } else {
    next(); // 确认转移
  }
});

router.afterEach((to, from, failure) => {
  if (isNavigationFailure(failure)) {
    console.log('failed navigation', failure)
  } else {
    to.meta.title ? (document.title = to.meta.title as string) : document.title = '知识图谱自动化构建平台'
  }
})

export default router;
