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

const router = createRouter({
  history: createWebHistory(),
  routes: [
    { path: '/login', component: () => import('../views/LoginView.vue') },
    { path: '/register', component: () => import('../views/RegisterView.vue') },
    { path: '/forgot-password', component: () => import('../views/ForgotPasswordView.vue') },
    {
      path: '/',
      component: () => import('../layouts/MainLayout.vue'),
      children: [
        // 公开页面 — 无需登录即可浏览
        { path: '', component: () => import('../views/HomeView.vue'), meta: { keepAlive: true } },
        { path: 'bet', component: () => import('../views/FootballView.vue'), meta: { keepAlive: true } },
        { path: 'football', redirect: '/bet' },
        { path: 'announcements', component: () => import('../views/AnnouncementListView.vue') },
        { path: 'announcements/:id', component: () => import('../views/AnnouncementDetailView.vue') },
        { path: 'messages', component: () => import('../views/InboxHubView.vue'), meta: { keepAlive: true, requiresAuth: false } },
        { path: 'messages/:id', component: () => import('../views/MessageDetailView.vue'), meta: { requiresAuth: true } },
        { path: 'match/:id', component: () => import('../views/MatchDetailView.vue') },
        // 需要登录的页面
        { path: 'bets', component: () => import('../views/MyBetsView.vue'), meta: { keepAlive: true, requiresAuth: true } },
        { path: 'bets/:betNo', component: () => import('../views/BetDetailView.vue'), meta: { requiresAuth: true } },
        { path: 'wallet', component: () => import('../views/WalletView.vue'), meta: { keepAlive: true, requiresAuth: true } },
        { path: 'wallet/detail', component: () => import('../views/WalletDetailView.vue'), meta: { requiresAuth: true } },
        { path: 'wallet/cashbacks', component: () => import('../views/CashbackRecordsView.vue'), meta: { requiresAuth: true } },
        { path: 'wallet/recharge', component: () => import('../views/RechargeView.vue'), meta: { requiresAuth: true } },
        { path: 'wallet/recharge/history', component: () => import('../views/RechargeHistoryView.vue'), meta: { requiresAuth: true } },
        { path: 'wallet/transactions/:transactionId', component: () => import('../views/WalletTransactionDetailView.vue'), meta: { requiresAuth: true } },
        { path: 'profile', component: () => import('../views/ProfileView.vue'), meta: { keepAlive: true, requiresAuth: true } },
        { path: 'profile/cashbacks', component: () => import('../views/CashbackRecordsView.vue'), meta: { requiresAuth: true } },
        { path: 'profile/edit', component: () => import('../views/ProfileEditView.vue'), meta: { requiresAuth: true } },
      ],
    },
  ],
});

router.beforeEach((to) => {
  const auth = useAuthStore();
  if ((to.path === '/login' || to.path === '/register' || to.path === '/forgot-password') && auth.token) return '/';
  // 需要登录的页面 — 未登录时弹出登录提示，留在当前页
  if (to.meta.requiresAuth && !auth.token) {
    auth.showLoginPrompt(to.fullPath);
    return false;
  }
});

export default router;
