import {createRouter, createWebHistory, RouterView} from 'vue-router'
import HomePage from "@/pages/home-page.vue";
import translation from "@/core/helpers/translations.js";
import {APP_NAME} from "@/config/index.js";
import NewPasswordForm from "@/components/forms/new-password-form.vue";
import BlogPage from "@/pages/blog-page.vue";
import PostPage from "@/pages/post-page.vue";
import ProfilePage from "@/pages/profile-page.vue";
import {useUserStore} from "@/stores/user.js";
import RegisterForm from "@/components/forms/register-form.vue";
import LoginForm from "@/components/forms/login-form.vue";
import ResetPasswordForm from "@/components/forms/reset-password-form.vue";
import StorePage from "@/pages/store-page.vue";
import ProductPage from "@/pages/product-page.vue";
import SearchPage from "@/pages/search-page.vue";
import CartPage from "@/pages/cart-page.vue";
import WishlistPage from "@/pages/wishlist-page.vue";

const routes = [
  {
    path: '/:locale?',
    component: RouterView,
    beforeEnter: translation.routeMiddleware,
    children: [
      {
        path: '',
        name: 'home',
        component: HomePage,
        meta: {
          title: "Home"
        }
      },
      {
        path: 'activate-user',
        name: 'activate-user',
        component: HomePage,
        meta: {
          title: 'Home'
        }
      },
      {
        path: 'blog',
        name: 'blog',
        component: BlogPage,
        meta: {
          title: 'Blog'
        }
      },
      {
        path: 'blog/post/:postSlug',
        name: 'blog-post',
        component: PostPage,
        meta: {
          title: 'Post'
        }
      },
      {
        path: 'store',
        name: 'store',
        component: StorePage,
        meta: {
          title: 'Store'
        }
      },
      {
        path: 'product/:productSlug',
        name: 'product',
        component: ProductPage,
        meta: {
          title: 'Product'
        }
      },
      {
        path: 'search',
        name: 'search',
        component: SearchPage,
        meta: {
          title: 'Search'
        }
      },
      {
        path: 'cart',
        name: 'cart',
        component: CartPage,
        meta: {
          title: 'Cart'
        }
      },
      {
        path: 'wishlist',
        name: 'wishlist',
        component: WishlistPage,
        meta: {
          title: 'Wishlist'
        }
      },
      {
        path: 'profile',
        name: 'profile',
        component: ProfilePage,
        meta: {
          title: 'Profile',
          requiresAuth: true
        }
      }
    ]
  }
]

const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes,
  scrollBehavior() {
    document.querySelector('#top').scrollIntoView({ behavior: 'smooth' })
    return {
      top: 0,
      left: 0,
      behavior: 'smooth'
    }
  }
})

router.beforeEach((to, from, next) => {
  const userStore = useUserStore();
  const isAuthenticated = userStore.accessToken

  document.title = to.meta.title ? `${APP_NAME} | ` + to.meta?.title : APP_NAME

  if (to.meta.requiresAuth && !isAuthenticated) {
    return next({
      name: 'home',
      query: { redirect: to.fullPath }
    });
  }

  if (to.meta.requiresGuest && isAuthenticated) {
    return next({ name: 'home' });
  }

  next();
})

export default router