import { createRouter, createWebHistory, RouteRecordRaw } from 'vue-router'
import Layout from '@/layouts/Layout.vue'

const routes: RouteRecordRaw[] = [
  {
    path: '/login',
    name: 'Login',
    component: () => import('@/views/Login.vue'),
    meta: { title: '登录', hidden: true }
  },
  {
    path: '/',
    component: Layout,
    redirect: '/dashboard',
    children: [
      {
        path: 'dashboard',
        name: 'Dashboard',
        component: () => import('@/views/Dashboard.vue'),
        meta: { title: '仪表盘', icon: 'DataLine' }
      }
    ]
  },
  {
    path: '/admin',
    component: Layout,
    redirect: '/admin/list',
    meta: { title: '管理员管理', icon: 'User' },
    children: [
      {
        path: 'list',
        name: 'AdminList',
        component: () => import('@/views/admin/AdminList.vue'),
        meta: { title: '管理员列表' }
      }
    ]
  },
  {
    path: '/user',
    component: Layout,
    redirect: '/user/list',
    meta: { title: '用户管理', icon: 'UserFilled' },
    children: [
      {
        path: 'list',
        name: 'UserList',
        component: () => import('@/views/user/UserList.vue'),
        meta: { title: '用户列表' }
      }
    ]
  },
  {
    path: '/aiconfig',
    component: Layout,
    redirect: '/aiconfig/list',
    meta: { title: 'AI配置管理', icon: 'Setting' },
    children: [
      {
        path: 'list',
        name: 'AiConfigList',
        component: () => import('@/views/aiconfig/AiConfigList.vue'),
        meta: { title: 'AI配置列表' }
      }
    ]
  },
  {
    path: '/dictionary',
    component: Layout,
    redirect: '/dictionary/list',
    meta: { title: '字典管理', icon: 'Document' },
    children: [
      {
        path: 'list',
        name: 'DictionaryList',
        component: () => import('@/views/dictionary/DictionaryList.vue'),
        meta: { title: '字典列表' }
      },
      {
        path: 'create',
        name: 'DictionaryCreate',
        component: () => import('@/views/dictionary/DictionaryForm.vue'),
        meta: { title: '创建字典', hidden: true }
      },
      {
        path: 'edit/:id',
        name: 'DictionaryEdit',
        component: () => import('@/views/dictionary/DictionaryForm.vue'),
        meta: { title: '编辑字典', hidden: true },
        props: true
      }
    ]
  },
  {
    path: '/tools',
    component: Layout,
    redirect: '/tools/api-tester',
    meta: { title: '开发工具', icon: 'Tools' },
    children: [
      {
        path: 'api-tester',
        name: 'ApiTester',
        component: () => import('@/views/tools/ApiTester.vue'),
        meta: { title: 'API接口调用' }
      }
    ]
  },
  {
    path: '/:pathMatch(.*)*',
    name: 'NotFound',
    component: () => import('@/views/NotFound.vue'),
    meta: { title: '404', hidden: true }
  }
]

const router = createRouter({
  history: createWebHistory('/emotion-museum-admin/'),
  routes
})

// 路由守卫
router.beforeEach((to, _from, next) => {
  const token = localStorage.getItem('adminToken')
  
  if (to.path === '/login') {
    if (token) {
      next('/')
    } else {
      next()
    }
  } else {
    if (token) {
      next()
    } else {
      next('/login')
    }
  }
})

export default router
