import { createRouter, createWebHistory, type RouteLocationNormalized } from 'vue-router';

// Admin auth constants
const ADMIN_TOKEN_KEY = 'gala_admin_token';
const ADMIN_ACCESS_CODE = '20268888';

// Auth guard for admin routes
function requireAdminAuth(to: RouteLocationNormalized) {
  const token = localStorage.getItem(ADMIN_TOKEN_KEY);
  if (!token || token !== generateToken(ADMIN_ACCESS_CODE)) {
    return { path: '/admin/login', query: { redirect: to.fullPath } };
  }
  return true;
}

// Simple token generator (not cryptographically secure, but sufficient for internal event)
function generateToken(code: string): string {
  return btoa(`gala2026:${code}:${code.split('').reverse().join('')}`);
}

// Export for use in login component
export { ADMIN_TOKEN_KEY, ADMIN_ACCESS_CODE, generateToken };

const router = createRouter({
  history: createWebHistory(),
  routes: [
    // ============================================
    // Big Screen Display Routes (LED PC)
    // ============================================
    {
      path: '/',
      name: 'screen-main',
      component: () => import('../views/MainDisplay.vue'),
      meta: { title: '年会大屏 - 主页' },
    },
    {
      path: '/screen/voting',
      name: 'screen-voting',
      component: () => import('../views/LiveVotingView.vue'),
      meta: { title: '年会大屏 - 实时投票' },
    },
    {
      path: '/screen/draw',
      name: 'screen-draw',
      component: () => import('../views/LuckyDrawView.vue'),
      meta: { title: '年会大屏 - 幸运抽奖' },
    },
    {
      path: '/screen/results',
      name: 'screen-results',
      component: () => import('../views/VoteResultsView.vue'),
      meta: { title: '年会大屏 - 投票结果' },
    },

    // Legacy routes (redirect to new paths)
    { path: '/voting', redirect: '/screen/voting' },
    { path: '/draw', redirect: '/screen/draw' },
    { path: '/results', redirect: '/screen/results' },

    // ============================================
    // Admin Routes (Director Console)
    // ============================================
    {
      path: '/admin/login',
      name: 'admin-login',
      component: () => import('../views/AdminLogin.vue'),
      meta: { title: '管理员登录' },
    },
    {
      path: '/admin/director-console',
      name: 'admin-console',
      component: () => import('../views/AdminControl.vue'),
      meta: { title: '导演控制台' },
      beforeEnter: requireAdminAuth,
    },

    // Legacy admin route (redirect)
    { path: '/admin', redirect: '/admin/director-console' },

    // ============================================
    // 404 Catch-all
    // ============================================
    {
      path: '/:pathMatch(.*)*',
      name: 'not-found',
      redirect: '/',
    },
  ],
});

// Update document title on navigation
router.afterEach((to) => {
  document.title = (to.meta.title as string) || '年会互动系统';
});

export default router;
