/**
 * 路由配置
 *
 * 基于 Vue Router 4 的 Hash 模式路由，
 * 包含登录、首页、个人中心等核心页面路由定义。
 * 通过全局前置守卫实现 Token 认证拦截。
 *
 * @module router
 */

import {
  createRouter,
  createWebHashHistory,
  type RouteRecordRaw,
  type NavigationGuardNext,
  type RouteLocationNormalized,
} from 'vue-router'

// ══════════════════════════════════════════════
// 常量
// ══════════════════════════════════════════════

/** 白名单路由 — 无需 Token 即可访问 */
const WHITE_LIST: string[] = ['/login']

// ══════════════════════════════════════════════
// 路由表
// ══════════════════════════════════════════════

/**
 * 动态路由（懒加载）
 *
 * 使用 import() 实现路由级别的代码分割，
 * 配合 Vite 自动生成独立的 chunk 文件。
 */
const routes: RouteRecordRaw[] = [
  {
    path: '/',
    redirect: '/home',
  },
  {
    path: '/login',
    name: 'Login',
    component: () => import('@/views/login/index.vue'),
    meta: {
      title: '登录',
      noAuth: true,
    },
  },
  {
    path: '/home',
    name: 'Home',
    component: () => import('@/views/home/index.vue'),
    meta: {
      title: '首页',
    },
  },
  {
    path: '/mine',
    name: 'Mine',
    component: () => import('@/views/mine/index.vue'),
    meta: {
      title: '我的',
    },
  },
  {
    path: '/mine/scanCode',
    name: 'ScanCode',
    component: () => import('@/views/mine/scanCode.vue'),
    meta: {
      title: '扫一扫',
    },
  },
  {
    path: '/mine/privacyPolicy',
    name: 'PrivacyPolicy',
    component: () => import('@/views/mine/privacyPolicy.vue'),
    meta: {
      title: '隐私政策',
    },
  },
  {
    path: '/mine/resetPwd',
    name: 'ResetPwd',
    component: () => import('@/views/mine/resetPwd.vue'),
    meta: {
      title: '修改密码',
    },
  },
  {
    path: '/noticeList',
    name: 'noticeList',
    component: () => import('@/views/notice/index.vue'),
    meta: {
      title: '消息通知',
    },
  },
  {
    path: '/menuEdit',
    name: 'menuEdit',
    component: () => import('@/views/menuEdit/index.vue'),
    meta: {
      title: '工作台设置',
    },
  },
  {
    path: '/addGroup/:title?',
    name: 'addGroup',
    component: () => import('@/views/menuEdit/addGroup.vue'),
    meta: {
      title: '分组管理',
    },
  },
  // ── 巡检模块 ──
  {
    path: '/inspection',
    name: 'Inspection',
    component: () => import('@/views/inspection/index.vue'),
    meta: {
      title: '巡检任务',
    },
  },
  {
    path: '/inspection/detail',
    name: 'InspectionDetail',
    component: () => import('@/views/inspection/detail.vue'),
    meta: {
      title: '任务详情',
    },
  },
  {
    path: '/inspectionRecords',
    name: 'InspectionRecords',
    component: () => import('@/views/inspectionRecords/index.vue'),
    meta: {
      title: '巡检记录',
    },
  },
  {
    path: '/inspectionRecords/detail',
    name: 'InspectionRecordsDetail',
    component: () => import('@/views/inspectionRecords/detail.vue'),
    meta: {
      title: '记录详情',
    },
  },
  // ── 问题工单模块 ──
  {
    path: '/inspectionProblem/:type?',
    name: 'InspectionProblem',
    component: () => import('@/views/inspectionProblem/index.vue'),
    meta: {
      title: '问题工单',
    },
  },
  {
    path: '/inspectionProblemDetail/:detail?/:obj?',
    name: 'InspectionProblemDetail',
    component: () => import('@/views/inspectionProblem/detail.vue'),
    meta: {
      title: '工单详情',
    },
  },
  {
    path: '/inspectionProblemManagement/:detail?/:obj?',
    name: 'InspectionProblemManagement',
    component: () => import('@/views/inspectionProblem/management.vue'),
    meta: {
      title: '工单管理',
    },
  },
  {
    path: '/maintenance/:type?',
    name: 'Maintenance',
    component: () => import('@/views/maintenance/index.vue'),
    meta: {
      title: '养护管理',
    },
  },
  // ── 养护模块 ──
  {
    path: '/maintenanceDetail/:detail?',
    name: 'MaintenanceDetail',
    component: () => import('@/views/maintenance/detail.vue'),
    meta: {
      title: '养护详情',
    },
  },
  {
    path: '/maintenanceRecords',
    name: 'MaintenanceRecords',
    component: () => import('@/views/maintenanceRecords/index.vue'),
    meta: {
      title: '养护记录',
    },
  },
  {
    path: '/maintenanceRecordsDetail/:detail?',
    name: 'MaintenanceRecordsDetail',
    component: () => import('@/views/maintenanceRecords/detail.vue'),
    meta: {
      title: '养护记录详情',
    },
  },
  {
    path: '/maintenanceCheck',
    name: 'MaintenanceCheck',
    component: () => import('@/views/maintenanceCheck/index.vue'),
    meta: {
      title: '养护检查',
    },
  },
  {
    path: '/maintenanceCheckRecords',
    name: 'MaintenanceCheckRecords',
    component: () => import('@/views/maintenanceCheckRecords/index.vue'),
    meta: {
      title: '养护检查记录',
    },
  },
  {
    path: '/maintenanceCheckRecords/detail',
    name: 'MaintenanceCheckRecordsDetail',
    component: () => import('@/views/maintenanceCheckRecords/detail.vue'),
    meta: {
      title: '检查记录详情',
    },
  },
  // ── 第三方监督模块 ──
  {
    path: '/supervisor',
    name: 'Supervisor',
    component: () => import('@/views/supervisor/index.vue'),
    meta: {
      title: '第三方监督人员',
    },
  },
  {
    path: '/supervisor/detail',
    name: 'SupervisorDetail',
    component: () => import('@/views/supervisor/detail.vue'),
    meta: {
      title: '人员详情',
    },
  },
  {
    path: '/superviseRecord',
    name: 'SuperviseRecord',
    component: () => import('@/views/superviseRecord/index.vue'),
    meta: {
      title: '监督记录',
    },
  },
  {
    path: '/superviseRecord/detail',
    name: 'SuperviseRecordDetail',
    component: () => import('@/views/superviseRecord/detail.vue'),
    meta: {
      title: '记录详情',
    },
  },
  // ── 监测设备模块 ──
  {
    path: '/monitoringEquipment/:type?',
    name: 'MonitoringEquipment',
    component: () => import('@/views/monitoringEquipment/index.vue'),
    meta: {
      title: '监测设备',
    },
  },
  {
    path: '/equipmentInfo',
    name: 'EquipmentInfo',
    component: () => import('@/views/monitoringEquipment/equipmentInfo.vue'),
    meta: {
      title: '设备详情',
    },
  },
  {
    path: '/mapMonitoring',
    name: 'MapMonitoring',
    component: () => import('@/views/monitoringEquipment/mapMonitoring.vue'),
    meta: {
      title: '地图监控',
    },
  },
  {
    path: '/monitoringDetail',
    name: 'MonitoringDetail',
    component: () => import('@/views/monitoringEquipment/monitoringDetail.vue'),
    meta: {
      title: '监测详情',
    },
  },
  // ── 有限空间作业记录模块 ──
  {
    path: '/yxkjzyRecords',
    name: 'YxkjzyRecords',
    component: () => import('@/views/yxkjzyRecords/index.vue'),
    meta: {
      title: '有限空间作业记录',
    },
  },
  {
    path: '/yxkjzyRecordsDetail/:detail?',
    name: 'YxkjzyRecordsDetail',
    component: () => import('@/views/yxkjzyRecords/detail.vue'),
    meta: {
      title: '作业记录详情',
    },
  },
  // ── 问题上报模块 ──
  {
    path: '/problemReport',
    name: 'ProblemReport',
    component: () => import('@/views/problemReport/index.vue'),
    meta: {
      title: '问题上报',
    },
  },
  {
    path: '/reportFloodedPoints',
    name: 'ReportFloodedPoints',
    component: () => import('@/views/problemReport/reportFloodedPoints.vue'),
    meta: {
      title: '积淹点上报',
    },
  },
  {
    path: '/reportInspection',
    name: 'ReportInspection',
    component: () => import('@/views/problemReport/reportInspection.vue'),
    meta: {
      title: '巡检问题上报',
    },
  },
  {
    path: '/reportEquipmentRepair',
    name: 'ReportEquipmentRepair',
    component: () => import('@/views/problemReport/reportEquipmentRepair.vue'),
    meta: {
      title: '设备报修',
    },
  },
  {
    path: '/reportSupervise/:detail?',
    name: 'ReportSupervise',
    component: () => import('@/views/problemReport/reportSupervise.vue'),
    meta: {
      title: '督办问题上报',
    },
  },
  {
    path: '/reportProject/:detail?',
    name: 'ReportProject',
    component: () => import('@/views/problemReport/reportProject.vue'),
    meta: {
      title: '项目问题上报',
    },
  },
  {
    path: '/reportConstruction/:detail?',
    name: 'ReportConstruction',
    component: () => import('@/views/problemReport/reportConstruction.vue'),
    meta: {
      title: '施工问题上报',
    },
  },
  // ── 防汛管理模块 ──
  {
    path: '/groupsClockList/:id?/:detail?',
    name: 'GroupsClockList',
    component: () => import('@/views/fxgl/groupsClockList.vue'),
    meta: {
      title: '打卡记录',
    },
  },
  {
    path: '/teamList/:id?',
    name: 'TeamList',
    component: () => import('@/views/fxgl/teamList.vue'),
    meta: {
      title: '选择成员',
    },
  },
  {
    path: '/instructionList',
    name: 'InstructionList',
    component: () => import('@/views/fxgl/instructionList.vue'),
    meta: {
      title: '防汛指令',
    },
  },
  {
    path: '/instructionReceive',
    name: 'InstructionReceive',
    component: () => import('@/views/fxgl/instructionReceive.vue'),
    meta: {
      title: '指令详情',
    },
  },
  {
    path: '/materialList',
    name: 'MaterialList',
    component: () => import('@/views/fxgl/materialList.vue'),
    meta: {
      title: '物资管理',
    },
  },
  {
    path: '/managementInventory/:id?',
    name: 'ManagementInventory',
    component: () => import('@/views/fxgl/managementInventory.vue'),
    meta: {
      title: '出入库',
    },
  },
  // ── 工程管理模块 ──
  {
    path: '/constructionTracking',
    name: 'ConstructionTracking',
    component: () => import('@/views/xjgl/constructionList.vue'),
    meta: {
      title: '工程项目',
    },
  },
  {
    path: '/constructionDetail/:id?',
    name: 'ConstructionDetail',
    component: () => import('@/views/xjgl/constructionDetail.vue'),
    meta: {
      title: '项目详情',
    },
  },
  // ── 排水户管理模块 ──
  {
    path: '/pshList',
    name: 'PshList',
    component: () => import('@/views/pshgl/pshList.vue'),
    meta: {
      title: '排水户管理',
    },
  },
  {
    path: '/pshglDetail/:detail?',
    name: 'PshglDetail',
    component: () => import('@/views/pshgl/pshglDetail.vue'),
    meta: {
      title: '排水户详情',
    },
  },
  {
    path: '/pshProblemList/:id?',
    name: 'PshProblemList',
    component: () => import('@/views/pshgl/pshProblemList.vue'),
    meta: {
      title: '问题列表',
    },
  },
  {
    path: '/pshProblemDetail/:detail?/:obj?',
    name: 'PshProblemDetail',
    component: () => import('@/views/pshgl/pshProblemDetail.vue'),
    meta: {
      title: '问题详情',
    },
  },
  {
    path: '/pshTaskList/:detail?/:obj?',
    name: 'PshTaskList',
    component: () => import('@/views/pshgl/pshTaskList.vue'),
    meta: {
      title: '任务管理',
    },
  },
  {
    path: '/checkList',
    name: 'CheckList',
    component: () => import('@/views/pshgl/checkList.vue'),
    meta: {
      title: '检查记录',
    },
  },
  {
    path: '/pshCheckList/:id?/:obj?',
    name: 'PshCheckList',
    component: () => import('@/views/pshgl/pshCheckList.vue'),
    meta: {
      title: '选择排水户',
    },
  },
  {
    path: '/pshCheck/:id?/:detail?',
    name: 'PshCheck',
    component: () => import('@/views/pshgl/pshCheck.vue'),
    meta: {
      title: '检查报告',
    },
  },
  // ── 地图模块 ──
  {
    path: '/map',
    name: 'Map',
    component: () => import('@/views/map/index.vue'),
    meta: {
      title: '地图',
    },
  },
  // ── 项目管理模块 ──
  {
    path: '/projectManagement',
    name: 'ProjectManagement',
    component: () => import('@/views/projectManagement/index.vue'),
    meta: {
      title: '项目管理',
    },
  },
  {
    path: '/projectManagementDetail/:detail?',
    name: 'ProjectManagementDetail',
    component: () => import('@/views/projectManagement/detail.vue'),
    meta: {
      title: '项目详情',
    },
  },
  // ── 扫码入口模块 ──
  {
    path: '/pshDetail/:id?',
    name: 'PshDetail',
    component: () => import('@/views/clientQR/pshDetail.vue'),
    meta: {
      title: '排水户详情',
    },
  },
  {
    path: '/deviceDetails/:id?',
    name: 'DeviceDetails',
    component: () => import('@/views/clientQR/deviceDetails.vue'),
    meta: {
      title: '设备详情',
    },
  },
  // 404 兜底
  {
    path: '/:pathMatch(.*)*',
    name: 'NotFound',
    redirect: '/home',
  },
]

// ══════════════════════════════════════════════
// 创建路由实例
// ══════════════════════════════════════════════

const router = createRouter({
  // Hash 模式 — 兼容移动端 WebView 和微信内嵌浏览器
  history: createWebHashHistory(),
  routes,
})

// ══════════════════════════════════════════════
// 全局前置守卫 — 认证拦截
// ══════════════════════════════════════════════

router.beforeEach(
  (
    to: RouteLocationNormalized,
    _from: RouteLocationNormalized,
    next: NavigationGuardNext,
  ) => {
    // 更新页面标题
    if (to.meta.title) {
      document.title = `${to.meta.title} - 舆图智慧水务`
    }

    // 白名单路由直接放行
    if (WHITE_LIST.includes(to.path) || to.meta.noAuth) {
      return next()
    }

    // 检查 Token 是否存在
    const token = localStorage.getItem('Authorization')

    if (token && token !== '') {
      // 已登录 → 放行
      return next()
    }

    // 未登录 → 重定向到登录页，保存原目标路径
    next({
      path: '/login',
      query: {
        redirect: to.fullPath,
      },
    })
  },
)

export default router
