import { createRouter, createWebHistory } from 'vue-router'

const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: [
    { path: '/login', component: () => import('@/views/auth/LoginView.vue') },
    {
      path: '/',
      component: () => import('@/views/layout/MainLayout.vue'),
      meta: { requiresAuth: true },
      children: [
        { path: '', redirect: '/tenants' },
        { path: 'tenants', component: () => import('@/views/tenants/TenantListView.vue') },
        { path: 'tenants/:id', component: () => import('@/views/tenants/TenantDetailView.vue') },
        { path: 'statistics', component: () => import('@/views/statistics/StatisticsView.vue') },
        { path: 'service-requests', component: () => import('@/views/services/ServiceRequestsView.vue') },
        { path: 'apps', component: () => import('@/views/apps/AppListView.vue') },
        { path: 'apps/:appKey', component: () => import('@/views/apps/AppDetailView.vue') },
        { path: 'push', component: () => import('@/views/push/PushDiagnosticsView.vue') },
        { path: 'operation-logs', component: () => import('@/views/logs/OperationLogView.vue') },
        { path: 'risk-control', component: () => import('@/views/risk/RiskControlView.vue') },
        { path: 'system-logs', component: () => import('@/views/system/ServerLogsView.vue') },
      ],
    },
  ],
})

router.beforeEach((to) => {
  const token = localStorage.getItem('ops_token')
  if (to.meta.requiresAuth && !token) return '/login'
})

export default router
