import { setupLayouts } from 'virtual:generated-layouts'
import generatedRoutes from 'virtual:generated-pages'
import type { RouteRecordRaw } from 'vue-router'
import DashboardEntries from './modules/dashboard'
import SystemEntries from './modules/system'
import DatabaseEntries from './modules/database'
import ApiEntries from './modules/api'
import ModelEntries from './modules/model'
import BizEntries from './modules/biz'
import ModuleEntries from './modules/module'
import AppCenterEntries from '@/router/modules/app.center'
import type { Route } from '#/global'
import useSettingsStore from '@/store/modules/settings'

// 固定路由（默认路由）
const constantRoutes: RouteRecordRaw[] = [
  {
    path: '/login',
    name: 'login',
    component: () => import('@/views/login/login.vue'),
    meta: {
      whiteList: true,
      title: '登录',
      i18n: 'route.login',
    },
  },
  {
    path: '/:all(.*)*',
    name: 'notFound',
    component: () => import('@/views/error/[...all].vue'),
    meta: {
      title: '找不到页面',
    },
  },
]

// 系统路由
const systemRoutes: RouteRecordRaw[] = [
  {
    path: '/',
    component: () => import('@/layouts/index.vue'),
    meta: {
      title: () => useSettingsStore().settings.home.title,
      breadcrumb: false,
    },
    children: [
      {
        path: '',
        name: 'home',
        component: () => import('@/views/dashboard/index.vue'),
        meta: {
          title: () => useSettingsStore().settings.home.title,
          i18n: 'route.home',
          icon: 'ant-design:home-twotone',
          breadcrumb: false,
        },
      },
      {
        path: 'reload',
        name: 'reload',
        component: () => import('@/views/reload/reload.vue'),
        meta: {
          title: '重新加载',
          breadcrumb: false,
        },
      },
      {
        path: 'personal/setting',
        name: 'personalSetting',
        component: () => import('@/views/personal/setting.vue'),
        meta: {
          title: '个人设置',
          i18n: 'route.personal.setting',
          cache: 'personalEditPassword',
        },
      },
      {
        path: 'personal/edit/password',
        name: 'personalEditPassword',
        component: () => import('@/views/personal/edit.password.vue'),
        meta: {
          title: '修改密码',
          i18n: 'route.personal.editpassword',
        },
      },
      {
        path: 'personal/notification',
        name: 'personalNotification',
        component: () => import('@/views/personal/notification.vue'),
        meta: {
          title: '通知中心',
          i18n: 'route.personal.notification',
        },
      },
    ],
  },
]

// 动态路由（异步路由、导航栏路由）
const asyncRoutes: Route.recordMainRaw[] = [
  {
    meta: {
      title: '首页',
      icon: 'ep:monitor',
    },
    children: [
      ...DashboardEntries,
    ],
  },
  {
    meta: {
      title: '模型',
      icon: 'bx:bxs-component',
    },
    children: [
      ...ModelEntries,
    ],
  },
  {
    meta: {
      title: '数据',
      icon: 'ep:coin',
    },
    children: [
      ...DatabaseEntries,
    ],
  },
  {
    meta: {
      title: '事务',
      icon: 'ep:element-plus',
    },
    children: [
      ...BizEntries,
    ],
  },
  {
    meta: {
      title: 'API',
      icon: 'ep:grape',
    },
    children: [
      ...ApiEntries,
    ],
  },
  {
    meta: {
      title: '功能',
      icon: 'ic:twotone-auto-awesome',
    },
    children: [
      ...ModuleEntries,
    ],
  },
  {
    meta: {
      title: '应用',
      icon: 'ep:menu',
    },
    children: [
      ...AppCenterEntries,
    ],
  },
  {
    meta: {
      title: '系统',
      icon: 'ep:setting',
    },
    children: [
      ...SystemEntries,
    ],
  },
]

const constantRoutesByFilesystem = generatedRoutes.filter((item) => {
  return item.meta?.enabled !== false && item.meta?.constant === true
})

const asyncRoutesByFilesystem = setupLayouts(generatedRoutes.filter((item) => {
  return item.meta?.enabled !== false && item.meta?.constant !== true && item.meta?.layout !== false
}))

export {
  constantRoutes,
  systemRoutes,
  asyncRoutes,
  constantRoutesByFilesystem,
  asyncRoutesByFilesystem,
}
