import { createRouter, createWebHistory } from 'vue-router'
import type { RouteRecordRaw } from 'vue-router'
import { setupRouterGuards } from './guards'

// 扩展路由元信息类型
declare module 'vue-router' {
  interface RouteMeta {
    title?: string
    requiresAuth?: boolean
    permission?: string
    role?: string
    icon?: string
    hidden?: boolean
    keepAlive?: boolean
  }
}

const routes: RouteRecordRaw[] = [
  {
    path: '/',
    name: 'Home',
    component: () => import('@/views/Home/index.vue'),
    meta: {
      title: '首页',
      requiresAuth: false,
      icon: 'House'
    }
  },
  {
    path: '/chat',
    name: 'Chat',
    component: () => import('@/views/Chat/index.vue'),
    meta: {
      title: 'AI对话',
      requiresAuth: true,
      icon: 'ChatDotRound',
      keepAlive: true
    }
  },
  {
    path: '/chat-history',
    name: 'ChatHistory',
    component: () => import('@/views/ChatHistory/index.vue'),
    meta: {
      title: '聊天历史',
      requiresAuth: true,
      icon: 'Clock'
    }
  },
  {
    path: '/diary',
    name: 'Diary',
    component: () => import('@/views/Diary/index.vue'),
    meta: {
      title: '情绪日记',
      requiresAuth: true,
      icon: 'EditPen'
    }
  },
  {
    path: '/life-milestones',
    name: 'LifeMilestones',
    component: () => import('@/views/LifeMilestones/index.vue'),
    meta: {
      title: '人生里程碑',
      requiresAuth: true,
      icon: 'Trophy'
    }
  },
  {
    path: '/life-trajectory',
    name: 'LifeTrajectory',
    component: () => import('@/views/LifeTrajectory/index.vue'),
    meta: {
      title: '人生轨迹',
      requiresAuth: true,
      icon: 'TrendCharts'
    }
  },
  {
    path: '/messages',
    name: 'Messages',
    component: () => import('@/views/Messages/index.vue'),
    meta: {
      title: '消息中心',
      requiresAuth: true,
      icon: 'Message'
    }
  },
  {
    path: '/personal-dashboard',
    name: 'PersonalDashboard',
    component: () => import('@/views/PersonalDashboard/index.vue'),
    meta: {
      title: '个人仪表盘',
      requiresAuth: true,
      icon: 'DataBoard'
    }
  },
  {
    path: '/settings',
    name: 'Settings',
    component: () => import('@/views/Settings/index.vue'),
    meta: {
      title: '设置',
      requiresAuth: true,
      icon: 'Setting'
    }
  },
  {
    path: '/topic-tracker',
    name: 'TopicTracker',
    component: () => import('@/views/TopicTracker/index.vue'),
    meta: {
      title: '话题追踪',
      requiresAuth: true,
      icon: 'Search'
    }
  },
  // 兼容原有页面
  {
    path: '/emotion',
    name: 'Emotion',
    component: () => import('@/views/Emotion/index.vue'),
    meta: {
      title: '情绪管理',
      requiresAuth: true,
      icon: 'Sunny'
    }
  },
  {
    path: '/map',
    name: 'Map',
    component: () => import('@/views/Map/index.vue'),
    meta: {
      title: '情绪地图',
      requiresAuth: true,
      icon: 'Location'
    }
  },
  {
    path: '/social',
    name: 'Social',
    component: () => import('@/views/Social/index.vue'),
    meta: {
      title: '社交分享',
      requiresAuth: true,
      icon: 'Share'
    }
  },
  {
    path: '/analysis',
    name: 'Analysis',
    component: () => import('@/views/Analysis/index.vue'),
    meta: {
      title: '情绪分析',
      requiresAuth: true,
      icon: 'TrendCharts'
    }
  },
  {
    path: '/profile',
    name: 'Profile',
    component: () => import('@/views/Profile/index.vue'),
    meta: {
      title: '个人中心',
      requiresAuth: true,
      icon: 'User'
    }
  },
  // 认证相关页面
  {
    path: '/login',
    name: 'Login',
    component: () => import('@/views/Login/index.vue'),
    meta: {
      title: '登录',
      requiresAuth: false,
      hidden: true
    }
  },
  {
    path: '/register',
    name: 'Register',
    component: () => import('@/views/Register/index.vue'),
    meta: {
      title: '注册',
      requiresAuth: false,
      hidden: true
    }
  },
  {
    path: '/forgot-password',
    name: 'ForgotPassword',
    component: () => import('@/views/ForgotPassword/index.vue'),
    meta: {
      title: '重置密码',
      requiresAuth: false,
      hidden: true
    }
  },
  {
    path: '/reset-password',
    redirect: '/forgot-password',
    meta: { hidden: true }
  },
  // 调试页面（仅开发环境）
  {
    path: '/debug',
    name: 'Debug',
    component: () => import('@/views/Debug/index.vue'),
    meta: {
      title: '环境变量调试',
      requiresAuth: false,
      hidden: true
    }
  },
  {
    path: '/debug/websocket',
    name: 'WebSocketTest',
    component: () => import('@/views/Debug/WebSocketTest.vue'),
    meta: {
      title: 'WebSocket测试',
      requiresAuth: false,
      hidden: true
    }
  },

  // 错误页面
  {
    path: '/403',
    name: 'Forbidden',
    component: () => import('@/views/NotFound/index.vue'),
    meta: {
      title: '访问被拒绝',
      requiresAuth: false,
      hidden: true
    }
  },
  {
    path: '/404',
    name: 'NotFound',
    component: () => import('@/views/NotFound/index.vue'),
    meta: {
      title: '页面未找到',
      requiresAuth: false,
      hidden: true
    }
  },
  {
    path: '/:pathMatch(.*)*',
    redirect: '/404'
  }
]

const router = createRouter({
  history: createWebHistory(import.meta.env.PROD ? '/emotion-museum/' : '/'),
  routes,
  scrollBehavior(to, from, savedPosition) {
    // 路由切换时的滚动行为
    if (savedPosition) {
      return savedPosition
    } else {
      return { top: 0 }
    }
  }
})

// 设置路由守卫
setupRouterGuards(router)

export default router

// 导出路由相关工具
export { routes }
export type { RouteRecordRaw }