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

// Basic wrapper layout (we will implement this later)
// For now, just use RouterView
const routes: Array<RouteRecordRaw> = [
    {
        path: '/',
        redirect: '/home',
    },
    {
        path: '/login',
        name: 'Login',
        component: () => import('@/views/auth/Login.vue'),
        meta: { title: '登录' }
    },
    {
        path: '/register',
        name: 'Register',
        component: () => import('@/views/auth/Register.vue'),
        meta: { title: '注册' }
    },
    {
        path: '/home',
        name: 'Home',
        component: () => import('@/views/home/Home.vue'),
        meta: { title: '校园活动' }
    },
    {
        path: '/activity/:id',
        name: 'ActivityDetail',
        component: () => import('@/views/activity/ActivityDetail.vue'),
        meta: { title: '活动详情' }
    },
    {
        path: '/registrations/my',
        name: 'MyRegistrations',
        component: () => import('@/views/registration/MyRegistrations.vue'),
        meta: { title: '我的票券' }
    },
    {
        path: '/reviews/my',
        name: 'MyReviews',
        component: () => import('@/views/review/MyReviews.vue'),
        meta: { title: '我的评价' }
    },
    {
        path: '/admin/dashboard',
        name: 'AdminDashboard',
        component: () => import('@/views/admin/AdminDashboard.vue'),
        meta: { title: '管理后台' }
    },
    {
        path: '/admin/activity/new',
        name: 'CreateActivity',
        component: () => import('@/views/admin/ActivityEdit.vue'),
        meta: { title: '创建活动' }
    },
    {
        path: '/admin/activity/edit/:id',
        name: 'EditActivity',
        component: () => import('@/views/admin/ActivityEdit.vue'),
        meta: { title: '编辑活动' }
    },
    {
        path: '/admin/activity/checkin/:id',
        name: 'CheckinManagement',
        component: () => import('@/views/admin/CheckinManagement.vue'),
        meta: { title: '签到管理' }
    },
    {
        path: '/admin/activity/stats/:id',
        name: 'ActivityStats',
        component: () => import('@/views/admin/ActivityStats.vue'),
        meta: { title: '数据统计' }
    },
    {
        path: '/admin/activity/reviews/:id',
        name: 'ActivityReviews',
        component: () => import('@/views/admin/ActivityReviews.vue'),
        meta: { title: '活动评价' }
    },
    // Placeholder routes
    {
        path: '/user',
        name: 'User',
        component: () => import('@/views/user/UserProfile.vue'),
    }
];

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

router.beforeEach((to, _from, next) => {
    document.title = (to.meta.title as string) || 'Campus Activity System';

    // Check if route requires auth
    const publicPages = ['/login', '/register', '/home', '/'];
    const authRequired = !publicPages.includes(to.path) && !to.path.startsWith('/activity/');
    const loggedIn = localStorage.getItem('auth') ? JSON.parse(localStorage.getItem('auth')!).token : null;

    if (authRequired && !loggedIn) {
        next('/login');
    } else {
        next();
    }
});

export default router;
