import { createRouter, createWebHashHistory } from 'vue-router'

const routes = [
  {
    path: '/login',
    name: 'Login',
    component: () => import('../views/auth/LoginView.vue'),
    meta: { guest: true, title: '登录' }
  },
  // New surgical observatory shell
  {
    path: '/',
    component: () => import('../components/shell/AppShell.vue'),
    children: [
      { path: '', redirect: '/workspace' },
      { path: 'workspace', name: 'Workspace', component: () => import('../views/workspace/WorkspaceView.vue'), meta: { title: '工作台' } },
      { path: 'periods', name: 'Periods', component: () => import('../views/period/PeriodListView.vue'), meta: { title: '考核周期' } },
      { path: 'period/templates', name: 'KpiTemplates', component: () => import('../views/period/KpiTemplateView.vue'), meta: { title: '绩效模板' } },
      { path: 'okr/my', name: 'MyOkr', component: () => import('../views/okr/MyOkrView.vue'), meta: { title: '我的 OKR' }, props: route => ({ periodId: route.query.periodId ? Number(route.query.periodId) : null }) },
      { path: 'okr/team', name: 'TeamOkr', component: () => import('../views/okr/TeamOkrView.vue'), meta: { title: '团队 OKR' } },
      { path: 'okr/alignment', name: 'OkrAlignment', component: () => import('../views/okr/AlignmentView.vue'), meta: { title: 'OKR 对齐' } },
      { path: 'okr/execution', name: 'OkrExecution', component: () => import('../views/okr/OkrExecutionView.vue'), meta: { title: '执行进度' } },
      { path: 'scores/my', name: 'MyScores', component: () => import('../views/scoring/MyResultView.vue'), meta: { title: '我的绩效' } },
      { path: 'scores/history', name: 'ScoreHistory', component: () => import('../views/scoring/HistoryView.vue'), meta: { title: '历史绩效' } },
      { path: 'scores/review', name: 'ScoringList', component: () => import('../views/scoring/ScoringListView.vue'), meta: { title: '绩效打分' } },
      { path: 'scores/review/:userId', name: 'ScoringDetail', component: () => import('../views/scoring/ScoringEditView.vue'), meta: { title: '绩效考评' } },
      { path: 'scores/appeal', name: 'ScoreAppeals', component: () => import('../views/scoring/ScoreAppealView.vue'), meta: { title: '绩效申诉' } },
      { path: 'feedback', name: 'Feedback', component: () => import('../views/feedback/FeedbackView.vue'), meta: { title: '绩效沟通' } },
      { path: 'org', name: 'Org', component: () => import('../views/org/OrgView.vue'), meta: { title: '组织架构' } },
      { path: 'dimensions', name: 'Dimensions', component: () => import('../views/system/DimensionConfigView.vue'), meta: { title: '考核维度' } },
      { path: 'logs', name: 'SystemLogs', component: () => import('../views/system/LogView.vue'), meta: { title: '操作日志' } },
      { path: 'notifications', name: 'Notifications', component: () => import('../views/notification/NotificationView.vue'), meta: { title: '消息通知' } }
    ]
  },
  // Legacy shell — keep for backward compatibility
  {
    path: '/legacy',
    component: () => import('../components/MainLayout.vue'),
    children: [
      { path: 'scoring', name: 'ScoringLegacy', component: () => import('../views/scoring/ScoringListView.vue'), meta: { title: '绩效打分' } },
      { path: 'scoring/:userId', name: 'ScoringDetailLegacy', component: () => import('../views/scoring/ScoringEditView.vue'), meta: { title: '绩效考评' } },
      { path: 'kpi/templates', name: 'KpiTemplatesLegacy', component: () => import('../views/period/KpiTemplateView.vue'), meta: { title: '绩效模板' } },
      { path: 'system/dimensions', name: 'DimensionsLegacy', component: () => import('../views/system/DimensionConfigView.vue'), meta: { title: '考核维度' } },
      { path: 'system/logs', name: 'SystemLogsLegacy', component: () => import('../views/system/LogView.vue'), meta: { title: '操作日志' } },
      { path: 'score-appeals', name: 'ScoreAppealsLegacy', component: () => import('../views/scoring/ScoreAppealView.vue'), meta: { title: '绩效申诉' } }
    ]
  }
]

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

router.beforeEach((to, from, next) => {
  const token = localStorage.getItem('accessToken')
  if (to.meta.guest) {
    next()
  } else if (!token) {
    next('/login')
  } else {
    next()
  }
})

export default router
