import { createRouter, createWebHashHistory } from 'vue-router'
import { getAdminRole, hasAdminSession } from '@/utils/admin-auth'

const router = createRouter({
  history: createWebHashHistory(),
  routes: [
    {
      path: '/',
      redirect: '/tx/browser',
    },
    {
      path: '/tx',
      redirect: '/tx/browser',
    },
    {
      path: '/tx/browser',
      component: () => import('@/views/tx/TencentBrowserView.vue'),
    },
    {
      path: '/claim/:token',
      component: () => import('@/views/claim/ClaimView.vue'),
    },
    {
      path: '/admin',
      redirect: '/admin/dashboard',
    },
    {
      path: '/admin/login',
      component: () => import('@/views/admin/auth/AdminLoginView.vue'),
    },
    {
      path: '/admin',
      component: () => import('@/views/admin/layout/AdminLayout.vue'),
      children: [
        {
          path: '',
          redirect: '/admin/dashboard',
        },
        {
          path: 'dashboard',
          component: () => import('@/views/admin/dashboard/AdminDashboardView.vue'),
        },
        {
          path: 'users',
          meta: { allowedRoles: ['admin'] },
          component: () => import('@/views/admin/users/AdminUsersView.vue'),
        },
        {
          path: 'orders',
          component: () => import('@/views/admin/orders/AdminOrdersView.vue'),
        },
        {
          path: 'orders/:orderId',
          component: () => import('@/views/admin/orders/AdminOrderDetailView.vue'),
        },
        {
          path: 'tasks',
          component: () => import('@/views/admin/tasks/AdminTasksView.vue'),
        },
        {
          path: 'manual-redeem',
          component: () => import('@/views/admin/manual-redeem/AdminManualRedeemView.vue'),
        },
        {
          path: 'tasks/:taskId',
          component: () => import('@/views/admin/tasks/AdminTaskDetailView.vue'),
        },
        {
          path: 'cdks',
          redirect: '/admin/inventory',
        },
        {
          path: 'inventory',
          meta: { allowedRoles: ['admin', 'operator'] },
          component: () => import('@/views/admin/inventory/AdminInventoryView.vue'),
        },
        {
          path: 'message-deliveries',
          component: () =>
            import('@/views/admin/message-deliveries/AdminMessageDeliveriesView.vue'),
        },
        {
          path: 'webhook-events',
          meta: { allowedRoles: ['admin', 'operator'] },
          component: () => import('@/views/admin/webhook-events/AdminWebhookEventsView.vue'),
        },
        {
          path: 'platform-shops',
          meta: { allowedRoles: ['admin'] },
          component: () => import('@/views/admin/platform-shops/AdminPlatformShopsView.vue'),
        },
        {
          path: 'platform-fulfillment',
          meta: { allowedRoles: ['admin'] },
          component: () => import('@/views/admin/fulfillment/AdminFulfillmentConfigHubView.vue'),
        },
        {
          path: 'platform-kuaishou-cloud-fulfillment',
          meta: { allowedRoles: ['admin'] },
          redirect: '/admin/platform-fulfillment?tab=kuaishou-cloud',
        },
        {
          path: 'webhook-events/:eventId',
          meta: { allowedRoles: ['admin', 'operator'] },
          component: () => import('@/views/admin/webhook-events/AdminWebhookEventDetailView.vue'),
        },
        {
          path: 'audit-logs',
          meta: { allowedRoles: ['admin'] },
          component: () => import('@/views/admin/audit-logs/AdminAuditLogsView.vue'),
        },
      ],
    },
    {
      path: '/:pathMatch(.*)*',
      component: () => import('@/views/NotFoundView.vue'),
    },
  ],
})

router.beforeEach((to) => {
  if (!to.path.startsWith('/admin')) {
    return true
  }

  if (to.path === '/admin/login') {
    if (hasAdminSession()) {
      return '/admin/dashboard'
    }

    return true
  }

  if (!hasAdminSession()) {
    return '/admin/login'
  }

  const allowedRoles = to.matched
    .map((record) => (record.meta?.allowedRoles as string[] | undefined) || [])
    .find((roles) => Array.isArray(roles) && roles.length > 0)

  if (allowedRoles && !allowedRoles.includes(getAdminRole())) {
    return '/admin/dashboard'
  }

  return true
})

export default router
