import type { RouteMeta } from 'vue-router';
import ElegantVueRouter from '@elegant-router/vue/vite';
import type { RouteKey } from '@elegant-router/types';

export function setupElegantRouter() {
  return ElegantVueRouter({
    layouts: {
      base: 'src/layouts/base-layout/index.vue',
      blank: 'src/layouts/blank-layout/index.vue'
    },
    customRoutes: {
      names: ['exception_403', 'exception_404', 'exception_500']
    },
    routePathTransformer(routeName, routePath) {
      const key = routeName as RouteKey;

      if (key === 'login') {
        const modules: UnionKey.LoginModule[] = ['pwd-login', 'reset-pwd'];

        const moduleReg = modules.join('|');

        return `/login/:module(${moduleReg})?`;
      }

      return routePath;
    },
    onRouteMetaGen(routeName) {
      const key = routeName as RouteKey;

      const constantRoutes: RouteKey[] = ['login', '403', '404', '500', 'workbench', 'feedback'];
      const routeMetaMap: Partial<Record<RouteKey, Partial<RouteMeta>>> = {
        workbench: {
          icon: 'mdi:view-dashboard-outline',
          order: 1,
          keepAlive: true
        },
        product: {
          icon: 'carbon:product',
          order: 4
        },
        product_list: {
          icon: 'material-symbols:view-list-outline-rounded',
          order: 1,
          keepAlive: true
        },
        product_dashboard: {
          hideInMenu: true,
          activeMenu: 'product_list'
        },
        product_requirement: {
          hideInMenu: true,
          activeMenu: 'product_list'
        },
        product_setting: {
          hideInMenu: true,
          activeMenu: 'product_list'
        },
        project: {
          icon: 'mdi:briefcase-outline',
          order: 5
        },
        project_list: {
          icon: 'material-symbols:view-list-outline-rounded',
          order: 1,
          keepAlive: true
        },
        project_project: {
          hideInMenu: true,
          activeMenu: 'project_list'
        },
        project_project_overview: {
          hideInMenu: true,
          activeMenu: 'project_list'
        },
        project_project_requirement: {
          hideInMenu: true,
          activeMenu: 'project_list'
        },
        project_project_execution: {
          hideInMenu: true,
          activeMenu: 'project_list'
        },
        project_project_setting: {
          hideInMenu: true,
          activeMenu: 'project_list'
        },
        ticket: {
          icon: 'mdi:ticket-confirmation-outline',
          order: 6
        },
        'ticket_my-submitted': {
          icon: 'mdi:upload-outline',
          order: 1,
          keepAlive: true
        },
        'ticket_my-pending': {
          icon: 'mdi:inbox-arrow-down-outline',
          order: 2,
          keepAlive: true
        },
        metrics: {
          icon: 'mdi:chart-line',
          order: 7
        },
        'metrics_project-progress': {
          icon: 'mdi:progress-clock',
          order: 1,
          keepAlive: true
        },
        'metrics_member-efficiency': {
          icon: 'mdi:account-multiple-check-outline',
          order: 2,
          keepAlive: true
        },
        metrics_worktime: {
          icon: 'mdi:clock-time-five-outline',
          order: 3,
          keepAlive: true
        },
        'personal-center': {
          icon: 'mdi:account-circle-outline',
          order: 8
        },
        'personal-center_my-profile': {
          icon: 'mdi:account-box-outline',
          order: 0,
          keepAlive: true
        },
        'personal-center_my-item': {
          icon: 'mdi:checkbox-multiple-blank-circle-outline',
          order: 1,
          keepAlive: true
        },
        'personal-center_work-report': {
          icon: 'mdi:file-chart-outline',
          order: 3,
          keepAlive: true
        },
        'personal-center_work-report_weekly': {
          hideInMenu: true,
          activeMenu: 'personal-center_work-report'
        },
        'personal-center_work-report_monthly': {
          hideInMenu: true,
          activeMenu: 'personal-center_work-report'
        },
        'personal-center_work-report_project': {
          hideInMenu: true,
          activeMenu: 'personal-center_work-report'
        },
        'personal-center_my-performance': {
          icon: 'mdi:trophy-outline',
          order: 4,
          keepAlive: true
        },
        'personal-center_my-application': {
          icon: 'mdi:file-document-outline',
          order: 5,
          keepAlive: true
        },
        'personal-center_overtime-application': {
          icon: 'mdi:clock-plus-outline',
          order: 6,
          keepAlive: true
        },
        'personal-center_pending-approval': {
          icon: 'mdi:check-decagram-outline',
          order: 7,
          keepAlive: true
        },
        system: {
          icon: 'carbon:cloud-service-management',
          order: 9,
          roles: ['R_ADMIN']
        },
        system_menu: {
          icon: 'material-symbols:route',
          order: 3,
          roles: ['R_ADMIN'],
          keepAlive: true
        },
        system_dict: {
          icon: 'mdi:book-open-page-variant-outline',
          order: 4,
          roles: ['R_ADMIN'],
          keepAlive: true
        },
        system_role: {
          icon: 'carbon:user-role',
          order: 2,
          roles: ['R_SUPER']
        },
        system_user: {
          icon: 'ic:round-manage-accounts',
          order: 1,
          roles: ['R_ADMIN']
        },
        'system_user-detail': {
          hideInMenu: true,
          roles: ['R_ADMIN'],
          activeMenu: 'system_user'
        },
        feedback: {
          icon: 'mdi:message-alert-outline',
          order: 10,
          keepAlive: true
        },
        infra: {
          icon: 'ep:monitor',
          order: 20
        },
        'infra_state-machine': {
          icon: 'mdi:state-machine',
          order: 1,
          keepAlive: true
        },
        'infra_log-management': {
          icon: 'mdi:text-box-search-outline',
          order: 2,
          keepAlive: true
        },
        'infra_log-management_login-log': {
          hideInMenu: true,
          activeMenu: 'infra_log-management'
        },
        'infra_log-management_operate-log': {
          hideInMenu: true,
          activeMenu: 'infra_log-management'
        },
        'infra_log-management_api-access-log': {
          hideInMenu: true,
          activeMenu: 'infra_log-management'
        },
        'infra_log-management_api-error-log': {
          hideInMenu: true,
          activeMenu: 'infra_log-management'
        },
        'infra_rd-code': {
          icon: 'mdi:identifier',
          order: 3,
          keepAlive: true
        }
      };

      const meta: Partial<RouteMeta> = {
        title: key,
        i18nKey: `route.${key}` as App.I18n.I18nKey,
        ...routeMetaMap[key]
      };

      if (constantRoutes.includes(key)) {
        meta.constant = true;
      }

      return meta;
    }
  });
}
