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

const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: [
    {
      path: '/login',
      name: 'login',
      component: () => import('../views/LoginView.vue')
    },
    {
      path: '/register',
      name: 'register',
      component: () => import('../views/RegisterView.vue')
    },
    {
      path: '/forgot-password',
      name: 'forgot-password',
      component: () => import('../views/ForgotPasswordView.vue')
    },
    {
      path: '/reset-password',
      name: 'reset-password',
      component: () => import('../views/ResetPasswordView.vue')
    },
    {
      path: '/public-feedback',
      name: 'public-feedback',
      component: () => import('../views/PublicFeedbackSubmitView.vue')
    },
    {
      path: '/',
      component: MainLayout,
      redirect: '/dashboard',
      meta: { requiresAuth: true },
      children: [
        {
          path: 'dashboard',
          name: 'dashboard',
          component: () => import('../views/HomeView.vue'),
          meta: { roles: ['ADMIN', 'DECISION_MAKER', 'SUPERVISOR'] }
        },
        {
          path: 'map',
          name: 'map',
          component: () => import('../views/GridManagementView.vue'),
          meta: { roles: ['ADMIN', 'DECISION_MAKER', 'SUPERVISOR', 'GRID_WORKER'] }
        },
        {
          path: 'tasks',
          name: 'tasks',
          component: () => import('../views/TaskView.vue'),
          meta: { roles: ['ADMIN', 'SUPERVISOR'] }
        },
        {
          path: 'my-tasks',
          name: 'my-tasks',
          component: () => import('../views/MyTasksView.vue'),
          meta: { roles: ['GRID_WORKER'] }
        },
        {
          path: 'worker-map',
          name: 'worker-map',
          component: () => import('../views/WorkerMapView.vue'),
          meta: { requiresAuth: true, roles: ['GRID_WORKER'] }
        },
        {
          path: 'my-tasks/:id',
          name: 'my-task-detail',
          component: () => import('../views/TaskDetailView.vue'),
          props: true,
          meta: { roles: ['GRID_WORKER'] }
        },
        {
          path: 'feedback',
          name: 'feedback',
          component: () => import('../views/FeedbackView.vue'),
          meta: { roles: ['PUBLIC_SUPERVISOR', 'ADMIN', 'SUPERVISOR'] }
        },
        {
          path: 'submit-feedback',
          name: 'submit-feedback',
          component: () => import('../views/SubmitFeedbackView.vue'),
          meta: { roles: ['PUBLIC_SUPERVISOR'] }
        },
        {
          path: 'system/users',
          name: 'system-users',
          component: () => import('../views/UserManagementView.vue'),
          meta: { roles: ['ADMIN'] }
        },
        {
          path: 'system/roles',
          name: 'system-roles',
          component: () => import('../views/OperationLogView.vue'),
          meta: { roles: ['ADMIN'] }
        },
        {
          path: 'settings/system',
          name: 'settings-system',
          component: () => import('../views/SystemSettingsView.vue'),
          meta: { roles: ['ADMIN'] }
        },
        {
          path: 'settings/profile',
          name: 'settings-profile',
          component: () => import('../views/UserProfileView.vue'),
          meta: { roles: ['ADMIN', 'DECISION_MAKER', 'SUPERVISOR', 'GRID_WORKER', 'PUBLIC_SUPERVISOR'] }
        },
        {
          path: 'settings/my-logs',
          name: 'settings-my-logs',
          component: () => import('../views/MyOperationLogView.vue'),
          meta: { roles: ['ADMIN', 'DECISION_MAKER', 'SUPERVISOR', 'GRID_WORKER', 'PUBLIC_SUPERVISOR'] }
        },
        {
          path: 'about',
          name: 'about',
          component: () => import('../views/AboutView.vue')
        }
      ]
    }
  ]
})

router.beforeEach((to, from, next) => {
  const publicPages = ['/login', '/register', '/forgot-password', '/reset-password', '/public-feedback'];
  const authRequired = !publicPages.includes(to.path);
  const authStore = useAuthStore();

  if (authRequired && !authStore.isLoggedIn) {
    return next('/login');
  }

  // 检查路由是否需要特定角色
  if (to.meta.roles) {
    const userRole = authStore.user?.role;
    if (userRole && (to.meta.roles as string[]).includes(userRole)) {
      next();
    } else {
      // 如果用户角色不被允许，可以重定向到 403 页面或主页
      // 这里我们简单地重定向到用户各自的主页
      if(userRole) {
        switch (userRole) {
          case 'GRID_WORKER':
            return next('/my-tasks');
          case 'PUBLIC_SUPERVISOR':
            return next('/submit-feedback');
          default:
            return next('/dashboard');
        }
      }
      return next('/login'); // 如果没有角色信息，则返回登录页
    }
  } else {
    // 如果路由没有 meta.roles，则允许所有已登录用户访问
    next();
  }
});

export default router
