import { createRouter, createWebHistory } from 'vue-router'
import Layout from '@/layout/index.vue'
import dayjs from 'dayjs'


const routes = [
  {
    path: '/login',
    name: 'Login',
    component: () => import('@/views/login/index.vue'),
    meta: { title: '登录', hideInMenu: true }
  },
  {
    path: '/',
    component: Layout,
    redirect: '/dashboard',
    children: [
      {
        path: 'dashboard',
        name: 'Dashboard',
        component: () => import('@/views/dashboard/index.vue'),
        meta: { title: '控制台', icon: 'Monitor' }
      },
      
      {
        path: 'account-group',
        name: 'AccountGroup',
        component: () => import('@/views/account-group/index.vue'),
        meta: { title: '账号组管理', icon: 'Grid' }
      },
      
      {
        path: 'ad-plan-conf',
        name: 'AdPlanConf',
        component: () => import('@/views/ad-plan-conf/index.vue'),
        meta: { title: '广告计划配置', icon: 'Setting' }
      },
      {
        path: 'ad-plan-conf/detail',
        name: 'AdPlanConfDetail',
        component: () => import('@/views/ad-plan-conf/detail.vue'),
        meta: { title: '广告计划配置详情', icon: 'Document', hideInMenu: true, noCache: true }
      },
      {
        path: 'ad-auto-conf',
        name: 'AdAutoConf',
        component: () => import('@/views/ad-auto-conf/index.vue'),
        meta: { title: '广告自动划配置', icon: 'Setting', noCache: true }
      },
      {
        path: 'ad-auto-conf/edit',
        name: 'AdAutoConfEdit',
        component: () => import('@/views/ad-auto-conf/edit.vue'),
        meta: { title: '编辑广告自动划配置', icon: 'Setting', hideInMenu: true, noCache: true }
      },
      {
        path: 'oss-file',
        name: 'OssFile',
        component: () => import('@/views/oss-file/index.vue'),
        meta: { title: 'OSS文件管理', icon: 'FolderOpened' }
      },
      {
        path: 'ad-plan-creative',
        name: 'AdPlanCreative',
        component: () => import('@/views/ad-plan-creative/index.vue'),
        meta: { title: '广告计划创意', icon: 'Picture', noCache: true }
      },
      {
        path: 'profile',
        name: 'Profile',
        component: () => import('@/views/profile/index.vue'),
        meta: { title: '个人中心', icon: 'User', hideInMenu: true }
      },
      {
        path: 'auth',
        name: 'Auth',
        component: () => import('@/views/auth/index.vue'),
        meta: { title: 'OAuth授权管理', icon: 'Lock' }
      },
      {
        path: 'user',
        name: 'User',
        component: () => import('@/views/user/index.vue'),
        meta: { title: '实名认证管理', icon: 'UserFilled' }
      },
      // {
      //   path: 'account',
      //   name: 'Account',
      //   component: () => import('@/views/account/index.vue'),
      //   meta: { title: '广告账户管理', icon: 'User' }
      // },
      
      {
        path: 'conversion',
        name: 'Conversion',
        component: () => import('@/views/conversion/index.vue'),
        meta: { title: '转化归因管理', icon: 'DataAnalysis' }
      },
      {
        path: 'daily-report',
        name: 'DailyReport',
        component: () => import('@/views/daily-report/index.vue'),
        meta: { title: '创意日报表', icon: 'TrendCharts' }
      },
      // {
      //   path: 'adgroup',
      //   name: 'Adgroup',
      //   component: () => import('@/views/adgroup/index.vue'),
      //   meta: { title: '广告组管理', icon: 'DataBoard' }
      // },
      {
        path: 'drama',
        name: 'Drama',
        component: () => import('@/views/drama/index.vue'),
        meta: { title: '短剧资产管理', icon: 'VideoPlay' }
      },
      {
        path: 'fiction',
        name: 'Fiction',
        component: () => import('@/views/fiction/index.vue'),
        meta: { title: '小说资产管理', icon: 'Reading' }
      },
      // {
      //   path: 'image',
      //   name: 'Image',
      //   component: () => import('@/views/image/index.vue'),
      //   meta: { title: '图片上传', icon: 'Picture' }
      // },
      // {
      //   path: 'video',
      //   name: 'Video',
      //   component: () => import('@/views/video/index.vue'),
      //   meta: { title: '视频上传', icon: 'VideoCamera' }
      // },
      
      {
        path: 'load-page-conf',
        name: 'LoadPageConf',
        component: () => import('@/views/load-page-conf/index.vue'),
        meta: { title: '落地页配置', icon: 'Document' }
      },
      {
        path: 'mokamvpuseradd',
        name: 'Tool',
        component: () => import('@/views/tools/index.vue'),
        meta: { title: '工具', icon: 'Document', hideInMenu: true }
      },
    ]
  }
]

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

// 路由守卫：检查登录状态
router.beforeEach((to, from, next) => {
  const storeUserInfo = localStorage.getItem('userInfo')
  
  // 如果是登录页面，直接放行
  if (to.path === '/login') {
    // 如果已登录，重定向到首页
    if (storeUserInfo) {
      next('/')
    } else {
      next()
    }
    return
  }
  
  // 其他页面需要检查登录状态
  if (!storeUserInfo) {
    next('/login')
  } else {
    const userInfo = JSON.parse(storeUserInfo)
    if (userInfo.userTokenExpiresAt && dayjs(userInfo.userTokenExpiresAt).isBefore(dayjs())) {
      // 第三方平台实名认证已过期，强制跳转个人中心重新认证
      // 若目标已是 /profile，直接放行，避免无限重定向
      if (to.path === '/profile') {
        next()
      } else {
        next('/profile')
      }
    } else {
      next()
    }
  }
})

export default router
