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

const routes = [
  // 独立入口页
  { path: '/', name: 'Splash', component: () => import('@/views/SplashView.vue') },
  // 公开门户
  {
    path: '/',
    component: () => import('@/layouts/PortalLayout.vue'),
    children: [
      { path: 'home', name: 'Home', component: () => import('@/views/portal/HomeView.vue') },
      { path: 'jobs', name: 'JobList', component: () => import('@/views/portal/JobListView.vue') },
      { path: 'jobs/:id', name: 'JobDetail', component: () => import('@/views/portal/JobDetailView.vue') },
      { path: 'companies', name: 'CompanyList', component: () => import('@/views/portal/CompanyListView.vue') },
      { path: 'companies/:id', name: 'CompanyDetail', component: () => import('@/views/portal/CompanyDetailView.vue') },
    ]
  },
  { path: '/login', name: 'Login', component: () => import('@/views/auth/LoginView.vue') },
  { path: '/register', name: 'Register', component: () => import('@/views/auth/RegisterView.vue') },
  { path: '/forgot-password', name: 'ResetPassword', component: () => import('@/views/auth/ResetPasswordView.vue') },
  // 求职者中心
  {
    path: '/seeker',
    component: () => import('@/layouts/SeekerLayout.vue'),
    meta: { requireAuth: true, role: 'seeker' },
    children: [
      { path: 'resume', name: 'SeekerResume', component: () => import('@/views/seeker/ResumeView.vue') },
      { path: 'applications', name: 'SeekerApplications', component: () => import('@/views/seeker/ApplicationsView.vue') },
      { path: 'favorites', name: 'SeekerFavorites', component: () => import('@/views/seeker/FavoritesView.vue') },
      { path: 'profile', name: 'SeekerProfile', component: () => import('@/views/seeker/ProfileView.vue') },
    ]
  },
  // 管理后台
  {
    path: '/admin',
    component: () => import('@/layouts/AdminLayout.vue'),
    meta: { requireAuth: true, role: 'admin' },
    children: [
      { path: 'jobs', name: 'AdminJobs', component: () => import('@/views/admin/JobManageView.vue') },
      { path: 'applications', name: 'AdminApplications', component: () => import('@/views/admin/ApplicationManageView.vue') },
      { path: 'organizations', name: 'AdminOrganizations', component: () => import('@/views/admin/OrganizationManageView.vue'), meta: { role: 'superadmin' } },
      { path: 'users', name: 'AdminUsers', component: () => import('@/views/admin/UserManageView.vue'), meta: { role: 'superadmin' } },
    ]
  },
  { path: '/:pathMatch(.*)*', redirect: '/' },
]

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

router.beforeEach(async (to, from, next) => {
  const auth = useAuthStore()

  if (to.meta.requireAuth) {
    if (!localStorage.getItem('access_token')) {
      return next({ name: 'Login', query: { redirect: to.fullPath } })
    }
    if (!auth.user) {
      try { await auth.fetchMe() } catch { return next({ name: 'Login' }) }
    }
    const requiredRole = to.meta.role
    if (requiredRole === 'seeker' && !auth.isSeeker) return next({ path: '/' })
    if (requiredRole === 'admin' && !(auth.isAdmin || auth.isSuperAdmin)) return next({ path: '/' })
    if (requiredRole === 'superadmin' && !auth.isSuperAdmin) return next({ path: '/admin/jobs' })
  }
  next()
})

export default router
