import { createRouter, createWebHistory } from 'vue-router'
import { useUserStore } from '@/stores/user'

const routes = [
  {
    path: '/login',
    name: 'Login',
    component: () => import('@/views/auth/Login.vue'),
    meta: { public: true },
  },
  {
    path: '/',
    name: 'Layout',
    component: () => import('@/components/Layout.vue'),
    redirect: '/dashboard',
    children: [
      {
        path: 'dashboard',
        name: 'Dashboard',
        component: () => import('@/views/dashboard/Dashboard.vue'),
        meta: { title: '首页', icon: 'HomeFilled' },
      },
      {
        path: 'datasource',
        name: 'DataSource',
        component: () => import('@/views/datasource/DataSource.vue'),
        meta: { title: '数据源管理', icon: 'DataLine' },
      },
      {
        path: 'metadata',
        name: 'Metadata',
        component: () => import('@/views/metadata/Metadata.vue'),
        meta: { title: '数据资产', icon: 'FolderOpened' },
      },
      {
        path: 'category',
        name: 'Category',
        component: () => import('@/views/category/Category.vue'),
        meta: { title: '分类分级标准', icon: 'Collection' },
      },
      {
        path: 'project',
        name: 'Project',
        component: () => import('@/views/project/Project.vue'),
        meta: { title: '项目管理', icon: 'List' },
      },
      {
        path: 'task',
        name: 'Task',
        component: () => import('@/views/task/Task.vue'),
        meta: { title: '我的任务', icon: 'EditPen' },
      },
      {
        path: 'classification',
        name: 'Classification',
        component: () => import('@/views/classification/Classification.vue'),
        meta: { title: '分类分级结果', icon: 'DocumentChecked' },
      },
      {
        path: 'report',
        name: 'Report',
        component: () => import('@/views/report/Report.vue'),
        meta: { title: '报表统计', icon: 'TrendCharts' },
      },
      {
        path: 'system',
        name: 'System',
        component: () => import('@/views/system/System.vue'),
        meta: { title: '系统管理', icon: 'Setting' },
      },
    ],
  },
  {
    path: '/:pathMatch(.*)*',
    redirect: '/',
  },
]

const router = createRouter({
  history: createWebHistory(),
  routes,
})

router.beforeEach((to, from, next) => {
  const userStore = useUserStore()
  if (!to.meta.public && !userStore.token) {
    next('/login')
  } else {
    next()
  }
})

export default router
