import { createRouter, createWebHistory } from 'vue-router'
import { useAuthStore } from '../stores/auth'

const routes = [
  {
    path: '/login',
    name: 'Login',
    component: () => import('../views/Login.vue'),
    meta: { title: '登录', public: true }
  },
  {
    path: '/',
    component: () => import('../layouts/AdminLayout.vue'),
    children: [
      {
        path: '',
        name: 'Dashboard',
        component: () => import('../views/Dashboard.vue'),
        meta: { title: '控制台' }
      },
      {
        path: 'users',
        name: 'Users',
        component: () => import('../views/users/UserList.vue'),
        meta: { title: '用户管理', permission: 'user:manage' }
      },
      {
        path: 'workspaces',
        name: 'Workspaces',
        component: () => import('../views/workspaces/WorkspaceList.vue'),
        meta: { title: '工作空间', permission: 'workspace:manage' }
      },
      {
        path: 'conversations',
        name: 'Conversations',
        component: () => import('../views/conversations/ConversationList.vue'),
        meta: { title: '对话管理', permission: 'conversation:manage' }
      },
      {
        path: 'sms-config',
        name: 'SMSConfig',
        component: () => import('../views/settings/SMSConfig.vue'),
        meta: { title: '短信平台配置', permission: 'sms_config' }
      },
      {
        path: 'payment-config',
        name: 'PaymentConfig',
        component: () => import('../views/settings/PaymentConfig.vue'),
        meta: { title: '支付配置', permission: 'payment_config' }
      },
      {
        path: 'sites',
        name: 'Sites',
        component: () => import('../views/sites/SiteList.vue'),
        meta: { title: '站点管理', permission: 'site:manage' }
      },
      {
        path: 'pages',
        name: 'Pages',
        component: () => import('../views/sites/PageList.vue'),
        meta: { title: '网页管理', permission: 'page:manage' }
      },
      {
        path: 'homepage-edit',
        name: 'HomepageEdit',
        component: () => import('../views/sites/HomepageEdit.vue'),
        meta: { title: '首页编辑', permission: 'homepage:edit' }
      },
      {
        path: 'files',
        name: 'FileManage',
        component: () => import('../views/files/FileManage.vue'),
        meta: { title: '文件管理', permission: null }
      },
      {
        path: 'role-permissions',
        name: 'RolePermissions',
        component: () => import('../views/settings/RolePermissions.vue'),
        meta: { title: '角色权限管理', permission: 'role:permission' }
      }
    ]
  }
]

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

router.beforeEach((to, from, next) => {
  const auth = useAuthStore()
  if (to.meta.public) {
    if (auth.isLoggedIn() && to.path === '/login') {
      next('/')
    } else {
      next()
    }
    return
  }
  if (!auth.isLoggedIn()) {
    next({ path: '/login', query: { redirect: to.fullPath } })
    return
  }
  if (to.meta.permission && !auth.hasPermission(to.meta.permission)) {
    next('/')
    return
  }
  next()
})

export default router
