import { createRouter, createWebHistory } from 'vue-router'
import HomeView from '../views/PlanView.vue'
import {API} from "@/services/api";

const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: [
    {
      path: '/:id',
      name: 'home',
      component: HomeView
    },
    {
      path: "/",
      name: "home-redirect",
      component: null,
      beforeEnter: async (to, from) => {
        const groups = (await API.getPlans())
            .sort((a, b) => a.id - b.id)
        return {
          name: "home",
          params: {
            id: groups.length > 0 ? groups[groups.length - 1].id : 0
          }
        }
      }
    }
  ]
})

export default router
