import { createRouter, createWebHistory } from 'vue-router'
import { useUserStore } from '../store/user'

// 导入页面组件
import Home from '../views/Home/index.vue'
import AIApplication from '../views/AIApplication/index.vue'
import Workflow from '../views/Workflow/index.vue'
import School from '../views/School/index.vue'
import Download from '../views/Download/index.vue'
// import Promotion from '../views/Promotion/index.vue'
import Membership from '../views/Membership/index.vue'
import CourseDetail from '../views/CourseDetail/index.vue'
import WorkflowDetail from '../views/WorkflowDetail/index.vue'
import Search from '../views/Search/index.vue'

const routes = [
  {
    path: '/',
    name: 'Home',
    component: Home,
    meta: {
      title: '首页 - 1818AI| Coze工作流复制下载 | AI视频生成 | AIGC创作平台 | 1818ai '
    }
  },
  {
    path: '/workflow',
    name: 'Workflow',
    component: Workflow,
    meta: {
      title: '工作流复制 - Coze工作流免费下载 | 数据分析工作流 | 1818ai'
    }
  },
  {
    path: '/ai-application',
    name: 'AIApplication',
    component: AIApplication,
    meta: {
      title: 'AI创作 - AI视频生成 | AIGC图文创作 | 1818ai',
      hideHeader: true,
      hideRightBar: true
    },
    children: [
      {
        path: '',
        redirect: '/ai-application/home'
      },
      {
        path: 'home',
        name: 'AIApplicationHome',
        component: () => import('../views/AIApplication/PlazaHome.vue'),
        meta: {
          title: 'AI创作首页 - AI视频生成广场 | AIGC作品分享 | 创意灵感 | 1818ai',
          hideHeader: true,
          hideRightBar: true
        }
      },
      {
        path: 'profile',
        name: 'AIApplicationProfile',
        component: () => import('../views/AIApplication/UserProfile.vue'),
        meta: {
          title: 'AI创作个人主页 - 我的AI作品 | 创作记录 | 作品管理 | 1818ai',
          hideHeader: true,
          hideRightBar: true
        }
      },
      {
        path: 'subscription',
        name: 'AIApplicationSubscription',  
        component: () => import('../views/AIApplication/Subscription.vue'),
        meta: {
          title: 'AI创作会员订阅 - 会员套餐 | 积分充值 | VIP权益 | 1818ai',
          hideHeader: true,
          hideRightBar: true
        }
      },
      {
        path: 'works',
        name: 'AIApplicationWorks',
        component: () => import('../views/AIApplication/Works.vue'),
        meta: {
          title: 'AI创作我的作品 - AI视频作品 | AIGC图片 | 创作历史 | 1818ai',
          hideHeader: true,
          hideRightBar: true
        }
      },
      {
        path: 'points',
        name: 'AIApplicationPoints',
        component: () => import('../views/AIApplication/Points.vue'),
        meta: {
          title: 'AI创作积分管理 - 积分充值 | 消费记录 | 积分兑换 | 1818ai',
          hideHeader: true,
          hideRightBar: true
        }
      },
      {
        path: 'text-to-video',
        name: 'AIApplicationTextToVideo',
        component: () => import('../views/AIApplication/TextToVideo.vue'),
        meta: {
          title: 'AI文生视频 - 文本生成视频 | AI视频制作 | 智能视频创作工具 | 1818ai',
          hideHeader: true,
          hideRightBar: true
        }
      },
      {
        path: 'image-to-video',
        name: 'AIApplicationImageToVideo',
        component: () => import('../views/AIApplication/ImageToVideo.vue'),
        meta: {
          title: 'AI图生视频 - 图片生成视频 | 静态图转动态视频 | AI视频工具 | 1818ai',
          hideHeader: true,
          hideRightBar: true
        }
      },
      {
        path: 'storyboard-video',
        name: 'AIApplicationStoryboardVideo',
        component: () => import('../views/AIApplication/StoryboardVideo.vue'),
        meta: {
          title: 'AI分镜视频 - 分镜脚本生成 | 故事板视频 | 1818ai',
          hideHeader: true,
          hideRightBar: true
        }
      },
      {
        path: 'create',
        name: 'AIApplicationCreate',
        component: () => import('../views/AIApplication/Create.vue'),
        meta: {
          title: 'AI视频创作 - 在线AI视频生成 | 智能视频制作 | AIGC创作 | 1818ai',
          hideHeader: true,
          hideRightBar: true,
          requiresAuth: true
        }
      },
      {
        path: 'image-create',
        name: 'AIApplicationImageCreate',
        component: () => import('../views/AIApplication/ImageCreate.vue'),
        meta: {
          title: 'AI图片创作 - AI绘画 | 智能图片生成 | AIGC图文创作 | 1818ai',
          hideHeader: true,
          hideRightBar: true,
          requiresAuth: true
        }
      },
      {
        path: 'work/:id',
        name: 'AIApplicationWorkDetail',
        component: () => import('../views/AIApplication/WorkDetail.vue'),
        meta: {
          title: 'AI作品详情 - AI视频作品 | AIGC创作展示 | 1818ai',
          hideHeader: true,
          hideRightBar: true
        }
      }
    ]
  },
  {
    path: '/school',
    name: 'School',
    component: School,
    meta: {
      title: '18学堂 - AI教程 | Coze工作流教程 | AIGC学习课程 | AI视频制作教学 | 1818ai'
    }
  },
  {
    path: '/download',
    name: 'Download',
    component: Download,
    meta: {
      title: '下载中心 - Coze工作流下载 | AI工具下载 | 1818ai'
    }
  },
  // {
  //   path: '/promotion',
  //   name: 'Promotion',
  //   component: Promotion,
  //   meta: {
  //     title: '拉新推广'
  //   }
  // },
  {
    path: '/membership',
    name: 'Membership',
    component: Membership,
    meta: {
      title: '购买会员 - 会员套餐 | VIP权益 | AI创作会员 | 积分充值 | 1818ai'
    }
  },
  {
    path: '/course/:id',
    name: 'CourseDetail',
    component: CourseDetail,
    meta: {
      title: '课程详情 - AI教程 | Coze工作流教学 | AIGC学习课程 | 1818ai'
    }
  },
  {
    path: '/workflow/:id',
    name: 'WorkflowDetail',
    component: WorkflowDetail,
    meta: {
      title: '工作流详情 - Coze工作流 | 智能体Bot | 数据分析工作流 | 1818ai'
    }
  },
  {
    path: '/search',
    name: 'Search',
    component: Search,
    meta: {
      title: '搜索结果 - Coze工作流搜索 | AI工具搜索 | 智能体Bot搜索 | 1818ai'
    }
  },
  // 个人中心路由
  {
    path: '/personal-center',
    name: 'PersonalCenter',
    component: () => import('../views/PersonalCenter/index.vue'),
    meta: {
      title: '个人中心 - 账户管理 | 我的订单 | 密钥管理 | 收藏夹 | 1818ai',
      requiresAuth: true
    },
    children: [
      {
        path: '',
        redirect: '/personal-center/key-management'
      },
      {
        path: 'key-management',
        name: 'KeyManagement',
        component: () => import('../views/PersonalCenter/KeyManagement.vue'),
        meta: {
          title: '密钥管理 - API密钥 | Coze密钥配置 | 工作流密钥设置 | 1818ai',
          requiresAuth: true
        }
      },
      {
        path: 'favorites',
        name: 'Favorites',
        component: () => import('../views/PersonalCenter/Favorites.vue'),
        meta: {
          title: '我的收藏 - 收藏的工作流 | 收藏的课程 | AI作品收藏 | 1818ai',
          requiresAuth: true
        }
      },
      {
        path: 'orders',
        name: 'Orders',
        component: () => import('../views/PersonalCenter/Orders.vue'),
        meta: {
          title: '订单管理 - 我的订单 | 购买记录 | 会员订单 | 积分订单 | 1818ai',
          requiresAuth: true
        }
      },
      {
        path: 'account',
        name: 'Account',
        component: () => import('../views/PersonalCenter/Account.vue'),
        meta: {
          title: '账户管理 - 个人信息 | 安全设置 | 账号绑定 | 1818ai',
          requiresAuth: true
        }
      },
      {
        path: 'promotion',
        name: 'Promotion',
        component: () => import('../views/PersonalCenter/Promotion.vue'),
        meta: {
          title: '推广拉新 - 邀请好友 | 推广奖励 | 分享赚积分 | 1818ai',
          requiresAuth: true
        }
      },
      {
        path: 'publish',
        name: 'Publish',
        component: () => import('../views/PersonalCenter/Publish.vue'),
        meta: {
          title: '发布作品 - AI作品发布 | 工作流分享 | 创作分享 | 1818ai',
          requiresAuth: true
        }
      },
      {
        path: 'test',
        name: 'Test',
        component: () => import('../views/PersonalCenter/Test.vue'),
        meta: {
          title: '页面测试 - 功能测试 | 开发调试 | 1818ai',
          requiresAuth: true
        }
      },
      {
        path: 'help',
        name: 'Help',
        component: () => import('../views/PersonalCenter/Help.vue'),
        meta: {
          title: '帮助中心 - 使用帮助 | 常见问题 | 操作指南 | 客服支持 | 1818ai',
          requiresAuth: true
        }
      }
    ]
  },
  // 404页面
  {
    path: '/:pathMatch(.*)*',
    name: 'NotFound',
    component: () => import('../views/NotFound/index.vue'),
    meta: {
      title: '页面未找到 - 404错误 | 返回首页 | 1818ai'
    }
  }
]

const router = createRouter({
  history: createWebHistory(),
  routes,
  scrollBehavior(to, from, savedPosition) {
    // 如果有保存的滚动位置（浏览器前进/后退），则恢复该位置
    if (savedPosition) {
      return savedPosition
    }
    
    // 检查是否是个人中心子路由之间的切换
    const isFromPersonalCenter = from.path.startsWith('/personal-center/')
    const isToPersonalCenter = to.path.startsWith('/personal-center/')
    
    // 如果是个人中心子路由之间的切换，保持当前滚动位置
    if (isFromPersonalCenter && isToPersonalCenter) {
      return undefined // 不改变滚动位置
    }
    
    // 否则直接滚动到页面顶部（无动画）
    return { top: 0 }
  }
})

// 路由守卫 - 设置页面标题和登录验证
router.beforeEach((to, from, next) => {
  // 设置页面标题
  if (to.meta.title && typeof to.meta.title === 'string') {
    document.title = `${to.meta.title} - 1818AI`
  }
  
  // 检测邀请码参数
  if (to.query.invite_code) {
    // 如果不是首页，自动导航到首页并保持邀请码参数
    if (to.path !== '/') {
      next({
        path: '/',
        query: { invite_code: to.query.invite_code }
      })
      return
    }
  }
  
  // 检查是否需要登录验证
  if (to.meta.requiresAuth) {
    const userStore = useUserStore()
    if (!userStore.getIsLoggedIn) {
      // 未登录，显示登录弹窗而不是跳转
      userStore.setShowLoginModal(true)
      // 中断当前导航
      next(false)
      return
    }
  }
  
  next()
})

export default router 