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

const routes: RouteRecordRaw[] = [
  {
    path: '/login',
    name: 'Login',
    component: () => import('@/views/Login.vue'),
    meta: { requiresAuth: false }
  },
  {
    path: '/',
    component: () => import('@/layouts/MainLayout.vue'),
    meta: { requiresAuth: true },
    children: [
      {
        path: '',
        redirect: '/dashboard'
      },
      {
        path: 'dashboard',
        name: 'Dashboard',
        component: () => import('@/views/Dashboard.vue'),
        meta: { title: '首页' }
      },
      {
        path: 'organizations',
        name: 'Organizations',
        component: () => import('@/views/organizations/OrganizationList.vue'),
        meta: { title: '组织管理', minLevel: 1 }
      },
      {
        path: 'allocations',
        name: 'Allocations',
        component: () => import('@/views/allocations/AllocationList.vue'),
        meta: { title: '物资配额' }
      },
      {
        path: 'allocations/categories',
        name: 'MaterialCategories',
        component: () => import('@/views/allocations/MaterialCategoryManagement.vue'),
        meta: { title: '物资类别管理', minLevel: 1 }
      },
      {
        path: 'allocations/create',
        name: 'AllocationCreate',
        component: () => import('@/views/allocations/AllocationForm.vue'),
        meta: { title: '创建配额', minLevel: 1 }
      },
      {
        path: 'allocations/:id/edit',
        name: 'AllocationEdit',
        component: () => import('@/views/allocations/AllocationForm.vue'),
        meta: { title: '编辑配额', minLevel: 1 }
      },
      {
        path: 'allocations/:id/report',
        name: 'AllocationReport',
        component: () => import('@/views/allocations/AllocationReport.vue'),
        meta: { title: '上报消耗' }
      },
      {
        path: 'allocations/change-requests',
        name: 'ChangeRequests',
        component: () => import('@/views/allocations/ChangeRequestList.vue'),
        meta: { title: '删改申请' }
      },
      {
        path: 'reports',
        name: 'Reports',
        component: () => import('@/views/reports/ReportList.vue'),
        meta: { title: '上报管理' }
      },
      {
        path: 'reports/summary',
        name: 'ReportSummary',
        component: () => import('@/views/reports/ReportSummary.vue'),
        meta: { title: '数据汇总' }
      },
      {
        path: 'personnel',
        name: 'Personnel',
        component: () => import('@/views/personnel/PersonnelList.vue'),
        meta: { title: '人才管理' }
      },
      {
        path: 'personnel/create',
        name: 'PersonnelCreate',
        component: () => import('@/views/personnel/PersonnelForm.vue'),
        meta: { title: '添加人才' }
      },
      {
        path: 'personnel/:id/edit',
        name: 'PersonnelEdit',
        component: () => import('@/views/personnel/PersonnelForm.vue'),
        meta: { title: '编辑人才' }
      },
      {
        path: 'personnel/:id',
        name: 'PersonnelDetail',
        component: () => import('@/views/personnel/PersonnelDetail.vue'),
        meta: { title: '人员详情' }
      },
      {
        path: 'approvals',
        name: 'Approvals',
        component: () => import('@/views/approvals/ApprovalList.vue'),
        meta: { title: '审批管理' }
      },
      {
        path: 'approvals/:id',
        name: 'ApprovalDetail',
        component: () => import('@/views/approvals/ApprovalDetail.vue'),
        meta: { title: '审批详情' }
      },
      {
        path: 'audit-logs',
        name: 'AuditLogs',
        component: () => import('@/views/AuditLogs.vue'),
        meta: { title: '操作记录', minLevel: 1 }
      }
    ]
  },
  {
    path: '/:pathMatch(.*)*',
    name: 'NotFound',
    component: () => import('@/views/NotFound.vue')
  }
]

const router = createRouter({
  history: createWebHistory('/corps/web/'),
  routes
})

router.beforeEach(async (to, _from, next) => {
  const authStore = useAuthStore()
  
  // 等待初始化完成
  if (!authStore.initialized) {
    await authStore.fetchProfile()
  }
  
  if (to.meta.requiresAuth !== false && !authStore.isAuthenticated) {
    next({ name: 'Login', query: { redirect: to.fullPath } })
    return
  }
  
  if (to.name === 'Login' && authStore.isAuthenticated) {
    next({ name: 'Dashboard' })
    return
  }
  
  // Check organizational level permission
  if (to.meta.minLevel && authStore.user) {
    const userLevel = authStore.user.organizationalLevel
    if (userLevel > (to.meta.minLevel as number)) {
      next({ name: 'Dashboard' })
      return
    }
  }
  
  next()
})

export default router
