import { createRouter, createWebHistory } from 'vue-router'
import { useAuthStore } from './stores/auth'

import AdminCandidatesView from './views/admin/AdminCandidatesView.vue'
import AdminDashboardView from './views/admin/AdminDashboardView.vue'
import AdminLayoutView from './views/admin/AdminLayoutView.vue'
import AdminReviewsView from './views/admin/AdminReviewsView.vue'
import AdminRiskView from './views/admin/AdminRiskView.vue'
import AdminSeasonsView from './views/admin/AdminSeasonsView.vue'
import HomeView from './views/HomeView.vue'
import NominationsView from './views/NominationsView.vue'
import VotingView from './views/VotingView.vue'
import WinnersView from './views/WinnersView.vue'

const router = createRouter({
  history: createWebHistory(),
  scrollBehavior(to, from, savedPosition) {
    if (savedPosition) {
      return savedPosition
    }

    if (to.path.startsWith('/admin') && from.path.startsWith('/admin')) {
      return false
    }

    return { top: 0 }
  },
  routes: [
    {
      path: '/',
      name: 'home',
      component: HomeView,
    },
    {
      path: '/nominations',
      name: 'nominations',
      component: NominationsView,
      meta: {
        requiresAuth: true,
        keepAlive: true,
      },
    },
    {
      path: '/voting',
      name: 'voting',
      component: VotingView,
      meta: {
        requiresAuth: true,
        keepAlive: true,
      },
    },
    {
      path: '/winners',
      name: 'winners',
      component: WinnersView,
    },
    {
      path: '/admin',
      component: AdminLayoutView,
      meta: {
        requiresAdmin: true,
      },
      children: [
        {
          path: '',
          redirect: { name: 'admin-dashboard' },
        },
        {
          path: 'dashboard',
          name: 'admin-dashboard',
          component: AdminDashboardView,
          meta: {
            keepAlive: true,
          },
        },
        {
          path: 'seasons',
          redirect: { name: 'admin-years' },
        },
        {
          path: 'years',
          name: 'admin-years',
          component: AdminSeasonsView,
          meta: {
            keepAlive: true,
          },
        },
        {
          path: 'candidates',
          name: 'admin-candidates',
          component: AdminCandidatesView,
          meta: {
            keepAlive: true,
          },
        },
        {
          path: 'reviews',
          name: 'admin-reviews',
          component: AdminReviewsView,
          meta: {
            keepAlive: true,
          },
        },
        {
          path: 'risk',
          name: 'admin-risk',
          component: AdminRiskView,
          meta: {
            keepAlive: true,
          },
        },
      ],
    },
  ],
})

router.beforeEach(async (to) => {
  const authStore = useAuthStore()

  if (!authStore.hydrated) {
    await authStore.hydrate()
  }

  if (to.meta.requiresAuth && !authStore.isLoggedIn) {
    return { name: 'home' }
  }

  if (to.meta.requiresAdmin && !authStore.isAdmin) {
    return { name: 'home' }
  }

  return true
})

export default router
