import { createRouter, createWebHistory } from 'vue-router'
import Login from '@/views/Login.vue'
import Register from '@/views/Register.vue'
import BookList from '@/views/BookList.vue'
import BookReader from '@/views/BookReader.vue'
import ReadingHistory from '@/views/ReadingHistory.vue'

const routes = [
  { path: '/login', component: Login },
  { path: '/register', component: Register },
  { path: '/', component: BookList, meta: { requiresAuth: false } },
  { path: '/book/:id', component: BookReader, meta: { requiresAuth: true } },
  { path: '/history', component: ReadingHistory, meta: { requiresAuth: true } },
]

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

// 路由守卫：检查登录状态
router.beforeEach((to, from, next) => {
  const token = localStorage.getItem('token')
  if (to.meta.requiresAuth && !token) {
    next('/login')
  } else {
    next()
  }
})

export default router
