import { createRouter, createWebHashHistory } from 'vue-router'
import { installRouteGuard } from '../utils/routeGuard.js'

// 固定活动存在多套线上历史地址；重定向时必须保留语言和 Banner 来源参数。
const redirectPreservingLocation = (path) => (to) => ({
  path,
  query: to.query,
  hash: to.hash,
})

const router = createRouter({
  history: createWebHashHistory(import.meta.env.BASE_URL),
  routes: [
    // 公共页面
    {
      path: '/',
      name: 'root',
      component: () => import('../views/LandingPage/LoadingView.vue'),
      meta: { requiresAuth: false, isPublic: true },
    },
    {
      path: '/loading',
      name: 'loading',
      component: () => import('../views/LandingPage/LoadingView.vue'),
      meta: { requiresAuth: false, isPublic: true },
    },
    {
      path: '/about',
      name: 'about',
      component: () => import('../views/LandingPage/AboutView.vue'),
    },
    {
      path: '/apply',
      name: 'apply',
      component: () => import('../views/ApplyHost/ApplyView.vue'),
      meta: { requiresAuth: false },
    },
    {
      path: '/not_app',
      name: 'not-app',
      component: () => import('../views/LandingPage/NotAppView.vue'),
      meta: { requiresAuth: false },
    },
    {
      path: '/platform-policy',
      name: 'platform-policy',
      component: () => import('../views/PlatformPolicy/PlatformPolicy.vue'),
      meta: { requiresAuth: true },
    },
    {
      path: '/invitation-to-register',
      name: 'legacy-invitation-to-register',
      // 线上旧 Banner 仍会打开该地址，必须转到当前邀请活动。
      redirect: redirectPreservingLocation('/invitation/invite-new-user'),
      meta: { requiresAuth: false, isPublic: true },
    },
    {
      path: '/ranking/king-queen',
      name: 'legacy-ranking-king-queen',
      // 兼容 atu_prod 和线上 Banner 使用的新版 King and Queen 地址。
      redirect: redirectPreservingLocation('/top-list'),
      meta: { requiresAuth: false, isPublic: true },
    },
    {
      path: '/ranking/weekly-star',
      name: 'legacy-ranking-weekly-star',
      // 兼容 atu_prod 和线上 Banner 使用的新版 Weekly Star 地址。
      redirect: redirectPreservingLocation('/weekly-star'),
      meta: { requiresAuth: false, isPublic: true },
    },
    {
      path: '/404',
      name: 'not-found-page',
      component: () => import('../views/LandingPage/NotFoundView.vue'),
      meta: { requiresAuth: false, isPublic: true },
    },
    {
      path: '/:pathMatch(.*)*',
      name: 'not-found',
      // 未知活动地址必须明确展示 404，不能伪装成公会/主播申请页。
      redirect: (to) => ({ path: '/404', query: { ...to.query, from: to.path } }),
    },

    // 管理员中心
    {
      path: '/admin-center',
      name: 'admin-center',
      component: () => import('../views/AdminCenter/index.vue'),
      meta: { requiresAuth: true },
    },
    {
      path: '/item-distribution',
      name: 'item-distribution',
      component: () => import('../views/AdminCenter/ItemDistribution.vue'),
      meta: { requiresAuth: true },
    },
    {
      path: '/my-BDLeader-teams',
      name: 'my-BDLeader-teams',
      component: () => import('../views/AdminCenter/MyBDLeaderTeams.vue'),
      meta: { requiresAuth: true },
    },
    {
      path: '/my-BD-teams',
      name: 'my-BD-teams',
      component: () => import('../views/AdminCenter/MyBDTeams.vue'),
      meta: { requiresAuth: true },
    },
    {
      path: '/my-agency-teams',
      name: 'my-agency-teams',
      component: () => import('../views/AdminCenter/MyAgencyTeams.vue'),
      meta: { requiresAuth: true },
    },
    {
      path: '/my-recharge-agency',
      name: 'my-recharge-agency',
      component: () => import('../views/AdminCenter/MyRechargeAgency.vue'),
      meta: { requiresAuth: true },
    },
    {
      path: '/admin-policy',
      name: 'admin-policy',
      component: () => import('../views/AdminCenter/policy.vue'),
      meta: { requiresAuth: true },
    },

    // BDLeader中心
    {
      path: '/bd-leader-center',
      name: 'bd-leader-center',
      component: () => import('../views/BDLeaderCenter/index.vue'),
      meta: { requiresAuth: true },
    },

    // BD中心
    {
      path: '/bd-center',
      name: 'bd-center',
      component: () => import('../views/BDCenter/index.vue'),
      meta: { requiresAuth: true },
    },
    {
      path: '/income-details',
      name: 'income-details',
      component: () => import('../views/BDCenter/incomeDetails.vue'),
      meta: { requiresAuth: true },
    },
    {
      path: '/policy',
      name: 'policy',
      component: () => import('../views/BDCenter/policy.vue'),
      meta: { requiresAuth: true },
    },
    {
      path: '/bd-item-distribution',
      name: 'bd-item-distribution',
      component: () => import('../views/BDCenter/ItemDistribution.vue'),
      meta: { requiresAuth: true },
    },

    // 金币代理页面
    {
      path: '/coin-seller',
      name: 'coin-seller',
      component: () => import('../views/RechargeAgency/index.vue'),
      meta: { requiresAuth: true },
    },
    {
      path: '/coin-seller-search',
      name: 'coin-seller-search',
      component: () => import('../views/RechargeAgency/SearchUser.vue'),
      meta: { requiresAuth: true },
    },
    {
      path: '/seller-records',
      name: 'seller-records',
      component: () => import('../views/RechargeAgency/SellerRecordsView.vue'),
      meta: { requiresAuth: true },
    },
    {
      path: '/coin-seller/sub-sellers',
      name: 'coin-seller-sub-sellers',
      component: () => import('../views/RechargeAgency/SubSellers.vue'),
      meta: { requiresAuth: true, requiresSuperDealer: true },
    },
    {
      path: '/recharge-agency-recruit',
      name: 'recharge-agency-recruit',
      component: () => import('../views/RechargeAgency/Recruit.vue'),
      meta: { requiresAuth: true },
    },

    // 代理中心
    {
      path: '/agency-center',
      name: 'agency-center',
      component: () => import('../views/AgencyCenter/index.vue'),
      meta: { requiresAuth: true },
    },
    {
      path: '/message',
      name: 'message',
      component: () => import('../views/AgencyCenter/Message.vue'),
      meta: { requiresAuth: true },
    },
    {
      path: '/agency-setting',
      name: 'agency-setting',
      component: () => import('../views/AgencyCenter/AgencySetting.vue'),
      meta: { requiresAuth: true },
    },
    {
      path: '/team-bill',
      name: 'team-bill',
      component: () => import('../views/AgencyCenter/TeamBill.vue'),
      meta: { requiresAuth: true },
    },
    {
      path: '/team-member',
      name: 'team-member',
      component: () => import('../views/AgencyCenter/TeamMember.vue'),
      meta: { requiresAuth: true },
    },
    {
      path: '/invite-members',
      name: 'invite-members',
      component: () => import('../views/AgencyCenter/InviteMembersProcess.vue'),
      meta: { requiresAuth: true },
    },

    // 主播中心
    {
      path: '/host-center',
      name: 'host-center',
      component: () => import('../views/HostCenter/index.vue'),
      meta: { requiresAuth: true },
    },
    {
      path: '/host-setting',
      name: 'host-setting',
      component: () => import('../views/HostCenter/HostSetting.vue'),
      meta: { requiresAuth: true },
    },

    // 靓号
    {
      path: '/good-ID',
      name: 'good-ID',
      component: () => import('../views/GoodID/index.vue'),
      meta: { requiresAuth: true },
    },

    // 收支相关页面
    {
      path: '/available-income',
      name: 'available-income',
      component: () => import('../views/Wallet/availableIncome.vue'),
      meta: { requiresAuth: true },
    },
    {
      path: '/transfer',
      name: 'transfer',
      component: () => import('../views/Wallet/Transfer/Transfer.vue'),
      meta: { requiresAuth: true },
    },
    {
      path: '/search-payee',
      name: 'search-payee',
      component: () => import('../views/Wallet/Transfer/SearchPayee.vue'),
      meta: { requiresAuth: true },
    },
    {
      path: '/exchange-gold-coins',
      name: 'exchange-gold-coins',
      component: () => import('../views/Wallet/Exchange/ExchangeGoldCoins.vue'),
      meta: { requiresAuth: true },
    },
    {
      path: '/information-details',
      name: 'information-details',
      component: () => import('../views/Wallet/Detail/InformationDetails.vue'),
      meta: { requiresAuth: true },
    },
    {
      path: '/history-salary',
      name: 'history-salary',
      component: () => import('../views/Wallet/Detail/HistorySalary.vue'),
      meta: { requiresAuth: true },
    },
    {
      path: '/pay-result',
      name: 'pay-result',
      component: () => import('../views/Wallet/PayResult/payResult.vue'),
      meta: { requiresAuth: true },
    },
    {
      path: '/cash-withdraw',
      name: 'cash-withdraw',
      component: () => import('../views/Wallet/CashOut/CashWithdraw.vue'),
      meta: { requiresAuth: true },
    },
    {
      path: '/cash-out',
      name: 'cash-out',
      component: () => import('../views/Wallet/CashOut/CashOut.vue'),
      meta: { requiresAuth: true },
    },
    {
      path: '/cash-out-details',
      name: 'cash-out-details',
      component: () => import('../views/Wallet/CashOut/Details.vue'),
      meta: { requiresAuth: true },
    },
    {
      path: '/KYC',
      name: 'KYC',
      component: () => import('../views/Wallet/CashOut/KYC.vue'),
      meta: { requiresAuth: true },
    },
    {
      path: '/bank-card',
      name: 'bank-card',
      component: () => import('../views/Wallet/CashOut/BankCard.vue'),
      meta: { requiresAuth: true },
    },

    // 充值页面
    {
      path: '/recharge',
      name: 'recharge',
      component: () => import('../views/Recharge/index.vue'),
      meta: { requiresAuth: true },
    },
    {
      path: '/recharge-pay-way',
      name: 'recharge-pay-way',
      component: () => import('../views/Recharge/payWay.vue'),
      meta: { requiresAuth: true },
    },
    {
      path: '/map',
      name: 'map',
      component: () => import('../views/Recharge/map.vue'),
      meta: { requiresAuth: true },
    },
    {
      path: '/recharge-guide',
      name: 'recharge-guide',
      component: () => import('../views/Recharge/guide.vue'),
      meta: { requiresAuth: true },
    },

    // 邀请页面
    // {
    //   path: '/invitation-to-register',
    //   name: 'invitation-to-register',
    //   component: () => import('../views/Invitation/InviteToRegister.vue'),
    //   meta: { requiresAuth: true },
    // },
    {
      path: '/invitation/invite-new-user',
      name: 'invitation-invite-new-user',
      component: () => import('../views/Invitation/inviteNewUser.vue'),
      meta: { requiresAuth: true },
    },
    {
      path: '/invite-bd-leader',
      name: 'invite-bd-leader',
      component: () => import('../views/Invitation/inviteBDLeader.vue'),
      meta: { requiresAuth: true },
    },
    {
      path: '/invite-bd',
      name: 'invite-bd',
      component: () => import('../views/Invitation/inviteBD.vue'),
      meta: { requiresAuth: true },
    },
    {
      path: '/invite-recharge-agency',
      name: 'invite-recharge-agency',
      component: () => import('../views/Invitation/inviteRechargeAgency.vue'),
      meta: { requiresAuth: true },
    },
    {
      path: '/invite-agency',
      name: 'invite-agency',
      component: () => import('../views/Invitation/inviteAgency.vue'),
      meta: { requiresAuth: true },
    },

    // 固定排行榜活动
    {
      path: '/couple',
      name: 'couple',
      component: () => import('../views/Ranking/Couple/index.vue'),
      meta: { requiresAuth: true },
    }, // 沙特每周一早上0点（北京时间5点）刷新
    {
      path: '/top-list',
      name: 'top-list',
      component: () => import('../views/Ranking/KingAndQueen/TopList.vue'),
      meta: { requiresAuth: true },
    }, // 沙特每周一早上0点（北京时间5点）刷新
    {
      path: '/weekly-star',
      name: 'weekly-star',
      component: () => import('../views/Ranking/WeeklyStar/WeeklyStar.vue'),
      meta: { requiresAuth: true },
    }, // 沙特每周一早上0点（北京时间5点）刷新
    {
      path: '/ranking',
      name: 'ranking',
      component: () => import('../views/Ranking/Overall/Ranking.vue'),
      meta: { requiresAuth: true },
    },
    {
      path: '/games-king',
      name: 'games-king',
      component: () => import('../views/Ranking/GamesKing/index.vue'),
      meta: { requiresAuth: true },
    }, //北京时间每周一早上5点刷新

    {
      path: '/cp-reward',
      name: 'cp-reward',
      component: () => import('../views/CPReward/index.vue'),
      meta: { requiresAuth: true },
    },

    // 固定活动
    {
      path: '/recharge-reward',
      name: 'recharge-reward',
      component: () => import('../views/Activities/Reward/RechargeReward.vue'),
      meta: { requiresAuth: true },
    },
    {
      path: '/login-reward',
      name: 'login-reward',
      component: () => import('../views/Activities/LoginReward/index.vue'),
      meta: { requiresAuth: true },
    },
    {
      path: '/daily-recharge',
      name: 'daily-recharge',
      component: () => import('../views/Activities/DailyRecharge/index.vue'),
      meta: { requiresAuth: true },
    },

    // 限时活动
    {
      path: '/activities/gulben-festival',
      name: 'gulben-festival',
      component: () => import('../views/Activities/GulbenFestival/index.vue'),
      meta: { requiresAuth: true },
    }, // 古尔邦节活动，2026年6月7日上午5点结束
    {
      path: '/activities/lucky-dollars-season5',
      name: 'lucky-dollars-season5',
      component: () => import('../views/Activities/LuckyDollars/Season5/index.vue'),
      meta: { requiresAuth: true },
    }, //2026年x月x日上午5点结束
    {
      path: '/activities/lucky-dollars-season4',
      name: 'lucky-dollars-season4',
      component: () => import('../views/Activities/LuckyDollars/Season4/index.vue'),
      meta: { requiresAuth: true },
    }, //2026年5月1日上午5点结束
    {
      path: '/activities/lesser-bairam',
      name: 'lesser-bairam',
      component: () => import('../views/Activities/LesserBairam/index.vue'),
      meta: { requiresAuth: true },
    }, //2026年4月4日上午5点结束
    {
      path: '/activities/lucky-dollars-season3',
      name: 'lucky-dollars-season3',
      component: () => import('../views/Activities/LuckyDollars/Season3/index.vue'),
      meta: { requiresAuth: true },
    }, //2026年3月19日上午5点结束（每周倒计时直到最后一周截止时间）
    {
      path: '/activities/spring-festival',
      name: 'spring-festival',
      component: () => import('../views/Activities/SpringFestival/index.vue'),
      meta: { requiresAuth: true },
    }, //2026年3月14日上午5点结束

    //游戏活动
    {
      path: '/activities/world-cup',
      name: 'world-cup',
      component: () => import('../views/Activities/WorldCup/index.vue'),
      meta: { requiresAuth: false },
    }, // WorldCup
    {
      path: '/activities/world-cup/event-detail',
      name: 'world-cup-event-detail',
      component: () => import('../views/Activities/WorldCup/EventDetail/index.vue'),
      meta: { requiresAuth: false },
    }, // WorldCup EventDetail
    {
      path: '/activities/games/the-greedy-king',
      name: 'games-the-greedy-king',
      component: () => import('../views/Activities/Games/TheGreedyKing/index.vue'),
      meta: { requiresAuth: true },
    }, //2025年xx月xx日上午5点结束
    {
      path: '/activities/bounty-football',
      name: 'bounty-football',
      component: () => import('../views/Activities/Games/BountyFootball/index.vue'),
      meta: { requiresAuth: false },
    }, //2026年6月12日上午5点结束
    {
      path: '/activities/poker-ace',
      name: 'pokerAce',
      component: () => import('../views/Activities/Games/PokerAce/index.vue'),
      meta: { requiresAuth: true },
    }, //2026年5月1日上午5点结束
  ],
})

// 安装路由守卫
installRouteGuard(router)

export default router
