import { createRouter, createWebHistory, type RouteRecordRaw } from 'vue-router'
import { getToken } from '@/utils/auth'
import NProgress from 'nprogress'
import 'nprogress/nprogress.css'

// NProgress 配置
NProgress.configure({ showSpinner: false })

const Layout = () => import('@/layout/index.vue')

const routes: RouteRecordRaw[] = [
  {
    path: '/login',
    name: 'Login',
    component: () => import('@/views/login/index.vue'),
    meta: { title: '登录', hidden: true }
  },
  {
    path: '/',
    component: Layout,
    redirect: '/dashboard',
    children: [
      {
        path: 'dashboard',
        name: 'Dashboard',
        component: () => import('@/views/dashboard/index.vue'),
        meta: { title: '仪表盘', icon: 'DataLine' }
      },
      {
        path: 'machine',
        name: 'Machine',
        component: () => import('@/views/machine/index.vue'),
        meta: { title: '机器管理', icon: 'Monitor' }
      },
      {
        path: 'camera',
        name: 'Camera',
        component: () => import('@/views/camera/index.vue'),
        meta: { title: '摄像头管理', icon: 'VideoCamera' }
      },
      {
        path: 'camera-vendor',
        name: 'CameraVendor',
        component: () => import('@/views/camera-vendor/index.vue'),
        meta: { title: '摄像头厂家', icon: 'OfficeBuilding' }
      },
      {
        path: 'lss',
        name: 'LSS',
        component: () => import('@/views/lss/index.vue'),
        meta: { title: 'LSS 管理', icon: 'Connection' }
      },
      {
        path: 'live-stream',
        name: 'LiveStream',
        component: () => import('@/views/live-stream/index.vue'),
        meta: { title: 'LiveStream 管理', icon: 'VideoCamera' }
      },
      {
        path: 'cloud',
        name: 'cloud',
        component: () => import('@/views/cc/cloud.vue'),
        meta: { title: 'cloud', icon: 'VideoCamera' }
      },
      {
        path: 'cloudflare',
        name: 'cloudflare',
        component: () => import('@/views/cc/cloudflare.vue'),
        meta: { title: 'cloudflare', icon: 'VideoCamera' }
      },
      {
        path: 'camera/channel/:deviceId',
        name: 'CameraChannel',
        component: () => import('@/views/camera/channel.vue'),
        meta: { title: '通道列表', hidden: true }
      },
      {
        path: 'camera/video/:deviceId/:channelId',
        name: 'CameraVideo',
        component: () => import('@/views/camera/video.vue'),
        meta: { title: '视频播放', hidden: true }
      },
      {
        path: 'streamtest',
        name: 'StreamTest',
        component: () => import('@/views/camera/stream-test.vue'),
        meta: { title: 'Stream 测试', icon: 'Monitor' }
      },
      {
        path: 'stream/videos',
        name: 'StreamVideos',
        component: () => import('@/views/stream/video-list.vue'),
        meta: { title: '视频管理', icon: 'Film' }
      },
      {
        path: 'stream/live',
        name: 'StreamLive',
        component: () => import('@/views/stream/live-list.vue'),
        meta: { title: '直播管理', icon: 'VideoCameraFilled' }
      },
      {
        path: 'stream/config',
        name: 'StreamConfig',
        component: () => import('@/views/stream/config.vue'),
        meta: { title: 'Stream 配置', icon: 'Setting' }
      },
      {
        path: 'user',
        name: 'User',
        component: () => import('@/views/user/index.vue'),
        meta: { title: '用户管理', icon: 'User' }
      },
      {
        path: 'cc',
        name: 'CloudflareStream',
        component: () => import('@/views/demo/cloudflareStream.vue'),
        meta: { title: 'Cloudflare Stream', icon: 'VideoCamera' }
      },
      {
        path: 'webrtc',
        name: 'WebrtcStream',
        component: () => import('@/views/demo/webrtc-stream.vue'),
        meta: { title: 'WebRTC 流', icon: 'Connection' }
      },
      {
        path: 'stats',
        name: 'Stats',
        component: () => import('@/views/stats/index.vue'),
        meta: { title: '观看统计', icon: 'DataAnalysis' }
      },
      {
        path: 'audit',
        name: 'Audit',
        component: () => import('@/views/audit/index.vue'),
        meta: { title: '审计日志', icon: 'Document' }
      },
      {
        path: 'demo/directurl',
        name: 'DirectUrl',
        component: () => import('@/views/demo/direct-url.vue'),
        meta: { title: '直接 URL', icon: 'Link' }
      },
      {
        path: 'demo/rtsp',
        name: 'RtspStream',
        component: () => import('@/views/demo/rtsp-stream.vue'),
        meta: { title: 'RTSP 流', icon: 'Connection' }
      },
      {
        path: 'demo/samples',
        name: 'SampleVideos',
        component: () => import('@/views/demo/sample-videos.vue'),
        meta: { title: '测试视频', icon: 'Film' }
      },
      {
        path: 'demo/hls',
        name: 'HlsStream',
        component: () => import('@/views/demo/hls-stream.vue'),
        meta: { title: 'M3U8/HLS', icon: 'VideoPlay' }
      },
      {
        path: 'monitor',
        name: 'Monitor',
        component: () => import('@/views/monitor/index.vue'),
        meta: { title: '多视频监控', icon: 'Monitor' }
      },
      {
        path: 'test/m-table',
        name: 'MTableDemo',
        component: () => import('@/views/test/m-table-demo.vue'),
        meta: { title: 'MTable 测试', icon: 'Grid' }
      }
    ]
  },
  {
    path: '/:pathMatch(.*)*',
    redirect: '/'
  }
]

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

// 白名单（无需登录即可访问）
const whiteList = ['/login', '/streamtest']

router.beforeEach((to, _from, next) => {
  // 开始进度条
  NProgress.start()

  // 统一使用系统名称作为标题
  document.title = '摄像头管理系统'

  const hasToken = getToken()

  if (hasToken) {
    if (to.path === '/login') {
      next({ path: '/' })
    } else {
      next()
    }
  } else if (whiteList.includes(to.path)) {
    next()
  } else {
    next(`/login?redirect=${to.fullPath}`)
  }
})

router.afterEach(() => {
  // 结束进度条
  NProgress.done()
})

export default router
