import { createRouter, createWebHashHistory } from "vue-router";
import DashboardView from "../views/DashboardView.vue";
import StudentsView from "../views/StudentsView.vue";
import ActivityView from "../views/ActivityView.vue";
import ResultsView from "../views/ResultsView.vue";
import HistoryView from "../views/HistoryView.vue";
import LoginView from "../views/LoginView.vue";
import UsersView from "../views/UsersView.vue";

const routes = [
  { path: "/login", name: "login", component: LoginView, meta: { public: true } },
  { path: "/", name: "dashboard", component: DashboardView },
  { path: "/students", name: "students", component: StudentsView },
  { path: "/activity", name: "activity", component: ActivityView },
  { path: "/results", name: "results", component: ResultsView },
  { path: "/history", name: "history", component: HistoryView },
  { path: "/users", name: "users", component: UsersView },
];

const router = createRouter({
  history: createWebHashHistory(),
  routes,
});

router.beforeEach((to) => {
  const token = localStorage.getItem("token");
  if (!to.meta.public && !token) return { name: "login" };
  if (to.name === "login" && token) return { name: "dashboard" };
});

export default router;
