import { createRouter, createWebHistory } from 'vue-router';
import { useAuthStore } from '../stores/auth';
import { ensureStaffSession } from '../utils/session-hydrate';

const router = createRouter({
  history: createWebHistory(),
  routes: [
    { path: '/login', component: () => import('../views/Login.vue'), meta: { public: true } },
    {
      path: '/',
      component: () => import('../layouts/ManageLayout.vue'),
      meta: { auth: true },
      children: [
        { path: '', component: () => import('../views/HomeEntry.vue') },
        {
          path: 'users',
          component: () => import('../views/AgentManager.vue'),
          meta: { adminOnly: true },
        },
        {
          path: 'agent-credit-transactions',
          component: () => import('../views/AgentCreditTransactions.vue'),
          meta: { adminOnly: true },
        },
        {
          path: 'agents',
          redirect: '/users',
        },
        {
          path: 'matches',
          component: () => import('../views/Matches.vue'),
          meta: { adminOnly: true },
        },
        {
          path: 'matches/outrights',
          component: () => import('../views/MatchesOutrights.vue'),
          meta: { adminOnly: true },
        },
        {
          path: 'matches/:matchId/edit',
          name: 'admin-match-edit',
          component: () => import('../views/matches/MatchEventEditor.vue'),
          meta: { adminOnly: true },
        },
        {
          path: 'matches/:matchId/markets',
          name: 'admin-match-markets',
          component: () => import('../views/matches/MatchMarketsPage.vue'),
          meta: { adminOnly: true },
        },
        { path: 'outrights', redirect: '/matches/outrights' },
        {
          path: 'outrights/:matchId/edit',
          name: 'admin-outright-edit',
          component: () => import('../views/outrights/OutrightEditRedirect.vue'),
          meta: { adminOnly: true },
        },
        { path: 'world-cup-outright', redirect: '/matches/outrights' },
        {
          path: 'bets',
          component: () => import('../views/Bets.vue'),
          meta: { adminOnly: true },
        },
        {
          path: 'settlement/:id',
          component: () => import('../views/Settlement.vue'),
          meta: { adminOnly: true },
        },
        {
          path: 'cashback',
          component: () => import('../views/Cashback.vue'),
          meta: { adminOnly: true },
        },
        {
          path: 'contents',
          component: () => import('../views/Contents.vue'),
          meta: { adminOnly: true },
        },
        {
          path: 'audit',
          component: () => import('../views/Audit.vue'),
          meta: { adminOnly: true },
        },
        {
          path: 'smoke-tests',
          component: () => import('../views/SmokeTests.vue'),
          meta: { adminOnly: true },
        },
        {
          path: 'media',
          component: () => import('../views/MediaLibrary.vue'),
          meta: { adminOnly: true },
        },
        {
          path: 'my-players',
          component: () => import('../views/agent/Players.vue'),
          meta: { agentOnly: true },
        },
        {
          path: 'sub-agents',
          redirect: '/my-players',
        },
        {
          path: 'my-bets',
          component: () => import('../views/agent/Bets.vue'),
          meta: { agentOnly: true },
        },
      ],
    },
  ],
});

router.beforeEach(async (to) => {
  const auth = useAuthStore();
  const hasToken = !!auth.token.value;

  if (hasToken) {
    await ensureStaffSession();
  }

  const hasUser = !!auth.user.value?.userType;

  if (to.meta.public) {
    if (hasToken && hasUser) return '/';
    return true;
  }

  if (!hasToken || !hasUser) {
    auth.clearStaffSession();
    return { path: '/login', query: { redirect: to.fullPath } };
  }

  if (to.meta.adminOnly && !auth.isAdmin.value) {
    return '/';
  }

  if (to.meta.agentOnly && !auth.isAgent.value) {
    return '/';
  }

  if (to.meta.tier1AgentOnly && !auth.isTier1Agent.value) {
    return '/';
  }

  return true;
});

export default router;
