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

const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: [
    {
      path: '/login',
      component: () => import('@/views/auth/LoginView.vue'),
    },
    {
      path: '/register',
      component: () => import('@/views/auth/RegisterView.vue'),
    },
    {
      path: '/forgot-password',
      component: () => import('@/views/auth/ForgotPasswordView.vue'),
    },
    {
      path: '/download/:appKey',
      component: () => import('@/views/download/DownloadView.vue'),
    },
    {
      path: '/',
      component: () => import('@/views/layout/MainLayout.vue'),
      meta: { requiresAuth: true },
      children: [
        {
          path: '',
          redirect: '/dashboard',
        },
        {
          path: 'dashboard',
          component: () => import('@/views/dashboard/DashboardView.vue'),
        },
        {
          path: 'apps',
          component: () => import('@/views/apps/AppListView.vue'),
        },
        {
          path: 'security',
          component: () => import('@/views/security/SecurityCenterView.vue'),
        },
        {
          path: 'docs',
          component: () => import('@/views/docs/DocsCenterView.vue'),
        },
        {
          path: 'operation-logs',
          component: () => import('@/views/logs/OperationLogView.vue'),
        },
        {
          path: 'apps/:appKey',
          component: () => import('@/views/apps/AppDetailView.vue'),
        },
        {
          path: 'apps/:appKey/im-config',
          component: () => import('@/views/im/ImConfigView.vue'),
        },
        {
          path: 'apps/:appKey/push-config',
          component: () => import('@/views/push/PushConfigView.vue'),
        },
        {
          path: 'apps/:appKey/push-management',
          component: () => import('@/views/push/PushManagementView.vue'),
        },
        {
          path: 'apps/:appKey/im-webhooks',
          component: () => import('@/views/im/ImWebhookView.vue'),
        },
        {
          path: 'apps/:appKey/im-webhooks/:webhookId/deliveries',
          component: () => import('@/views/im/WebhookDeliveryLogView.vue'),
        },
        {
          path: 'apps/:appKey/im-webhook-alerts',
          component: () => import('@/views/im/WebhookAlertView.vue'),
        },
        {
          path: 'apps/:appKey/im',
          component: () => import('@/views/im/ImManagementView.vue'),
        },
        {
          path: 'apps/:appKey/update',
          component: () => import('@/views/update/VersionManagementView.vue'),
        },
        {
          path: 'services/im/:appKey?',
          component: () => import('@/views/im/ImManagementView.vue'),
        },
        {
          path: 'services/push/:appKey?',
          component: () => import('@/views/push/PushManagementView.vue'),
        },
        {
          path: 'services/update/:appKey?',
          component: () => import('@/views/update/VersionManagementView.vue'),
        },
        {
          path: 'services/bugcollect/:appKey?',
          component: () => import('@/views/bug-collect/BugCollectOverview.vue'),
        },
        {
          path: 'system-logs',
          component: () => import('@/views/system/ServerLogsView.vue'),
        },
        {
          path: 'database',
          component: () => import('@/views/database/DatabaseView.vue'),
        },
        {
          path: 'accounts',
          component: () => import('@/views/accounts/SubAccountView.vue'),
        },
        // ── Bug 收集 ─────────────────────────────────────────────────
        {
          path: 'bugcollect/overview',
          component: () => import('@/views/bug-collect/BugCollectOverview.vue'),
        },
        {
          path: 'bugcollect/issues',
          component: () => import('@/views/bug-collect/BugCollectIssues.vue'),
        },
        {
          path: 'bugcollect/issues/:id',
          component: () => import('@/views/bug-collect/BugCollectIssueDetail.vue'),
        },
        {
          path: 'bugcollect/events',
          component: () => import('@/views/bug-collect/BugCollectEvents.vue'),
        },
        {
          path: 'bugcollect/funnels',
          component: () => import('@/views/bug-collect/BugCollectFunnels.vue'),
        },
        {
          path: 'bugcollect/webhooks',
          component: () => import('@/views/bug-collect/BugCollectWebhooks.vue'),
        },
        {
          path: 'bugcollect/sourcemaps',
          component: () => import('@/views/bug-collect/BugCollectSourcemaps.vue'),
        },
        {
          path: 'bugcollect/rank/freq',
          component: () => import('@/views/bug-collect/BugCollectRankFreq.vue'),
        },
        {
          path: 'bugcollect/rank/risk',
          component: () => import('@/views/bug-collect/BugCollectRankRisk.vue'),
        },
      ],
    },
  ],
})

router.beforeEach((to) => {
  const auth = useAuthStore()
  if (to.meta.requiresAuth && !auth.token) {
    return '/login'
  }
  if ((to.path === '/login' || to.path === '/register') && auth.token) {
    return '/dashboard'
  }
})

export default router
