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

const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: [
    {
      path: '/login',
      name: 'login',
      component: () => import('../views/LoginView.vue'),
      meta: { requiresAuth: false },
    },
    {
      path: '/balance',
      name: 'balance',
      component: () => import('../views/BalanceView.vue'),
      meta: { requiresAuth: true },
    },
    {
      path: '/email',
      name: 'email',
      component: () => import('../views/EmailRecord.vue'),
      meta: { requiresAuth: true },
    },
    {
      path: '/setting',
      name: 'setting',
      component: () => import('../views/SettingView.vue'),
      meta: { requiresAuth: true },
    },
    {
      path: '/calendar',
      name: 'calendar',
      component: () => import('../views/CalendarView.vue'),
      meta: { requiresAuth: true },
    },
    {
      path: '/smart-classification',
      name: 'smart-classification',
      component: () => import('../views/ClassificationSmart.vue'),
      meta: { requiresAuth: true },
    },
    {
      path: '/classification-edit',
      name: 'classification-edit',
      component: () => import('../views/ClassificationEdit.vue'),
      meta: { requiresAuth: true },
    },
    {
      path: '/classification-batch',
      name: 'classification-batch',
      component: () => import('../views/ClassificationBatch.vue'),
      meta: { requiresAuth: true },
    },
    {
      path: '/classification-nlp',
      name: 'classification-nlp',
      component: () => import('../views/ClassificationNLP.vue'),
      meta: { requiresAuth: true },
    },
    {
      path: '/',
      name: 'statistics',
      component: () => import('../views/StatisticsView.vue'),
      meta: { requiresAuth: true },
    },
    {
      path: '/bill-analysis',
      name: 'bill-analysis',
      component: () => import('../views/BillAnalysisView.vue'),
      meta: { requiresAuth: true },
    },
    {
      path: '/message',
      name: 'message',
      redirect: { path: '/balance', query: { tab: 'message' } },
      meta: { requiresAuth: true },
    },
    {
      path: '/periodic-record',
      name: 'periodic-record',
      component: () => import('../views/PeriodicRecord.vue'),
      meta: { requiresAuth: true },
    },
    {
      path: '/log',
      name: 'log',
      component: () => import('../views/LogView.vue'),
      meta: { requiresAuth: true },
    },
    {
      path: '/budget',
      name: 'budget',
      component: () => import('../views/BudgetView.vue'),
      meta: { requiresAuth: true },
    }
  ],
})

// 路由守卫
router.beforeEach((to, from, next) => {
  const authStore = useAuthStore()
  const requiresAuth = to.meta.requiresAuth !== false // 默认需要认证

  if (requiresAuth && !authStore.isAuthenticated) {
    // 需要认证但未登录，跳转到登录页
    next({ name: 'login', query: { redirect: to.fullPath } })
  } else if (to.name === 'login' && authStore.isAuthenticated) {
    // 已登录用户访问登录页，跳转到首页
    next({ name: 'transactions' })
  } else {
    next()
  }
})

export default router

