import Vue from "vue";
import Router from "vue-router";
import Login from "../views/Login.vue";
import Register from "../views/Register.vue";
import Layout from "../views/Layout.vue";

import AdminDashboard from "../views/admin/Dashboard.vue";
import AdminUsers from "../views/admin/Users.vue";
import AdminElders from "../views/admin/Elders.vue";
import AdminSchedules from "../views/admin/Schedules.vue";
import AdminBills from "../views/admin/Bills.vue";
import AdminFeedback from "../views/admin/Feedback.vue";
import AdminNotices from "../views/admin/Notices.vue";

import NurseDashboard from "../views/nurse/Dashboard.vue";
import NurseSchedules from "../views/nurse/Schedules.vue";
import NurseCare from "../views/nurse/CareRecords.vue";
import NurseHealth from "../views/nurse/HealthRecords.vue";
import NurseHandovers from "../views/nurse/Handovers.vue";
import NurseNotices from "../views/nurse/Notices.vue";

import FamilyDashboard from "../views/family/Dashboard.vue";
import FamilyElders from "../views/family/Elders.vue";
import FamilyCare from "../views/family/CareRecords.vue";
import FamilyHealth from "../views/family/HealthRecords.vue";
import FamilyBills from "../views/family/Bills.vue";
import FamilyFeedback from "../views/family/Feedback.vue";
import FamilyNotices from "../views/family/Notices.vue";

Vue.use(Router);

const router = new Router({
  mode: "history",
  routes: [
    { path: "/", redirect: "/login" },
    { path: "/login", component: Login },
    { path: "/register", component: Register },
    {
      path: "/admin",
      component: Layout,
      meta: { role: "ADMIN" },
      children: [
        { path: "", redirect: "dashboard" },
        { path: "dashboard", component: AdminDashboard },
        { path: "users", component: AdminUsers },
        { path: "elders", component: AdminElders },
        { path: "schedules", component: AdminSchedules },
        { path: "bills", component: AdminBills },
        { path: "feedback", component: AdminFeedback },
        { path: "notices", component: AdminNotices }
      ]
    },
    {
      path: "/nurse",
      component: Layout,
      meta: { role: "NURSE" },
      children: [
        { path: "", redirect: "dashboard" },
        { path: "dashboard", component: NurseDashboard },
        { path: "schedules", component: NurseSchedules },
        { path: "care", component: NurseCare },
        { path: "health", component: NurseHealth },
        { path: "handovers", component: NurseHandovers },
        { path: "notices", component: NurseNotices }
      ]
    },
    {
      path: "/family",
      component: Layout,
      meta: { role: "FAMILY" },
      children: [
        { path: "", redirect: "dashboard" },
        { path: "dashboard", component: FamilyDashboard },
        { path: "elders", component: FamilyElders },
        { path: "care", component: FamilyCare },
        { path: "health", component: FamilyHealth },
        { path: "bills", component: FamilyBills },
        { path: "feedback", component: FamilyFeedback },
        { path: "notices", component: FamilyNotices }
      ]
    }
  ]
});

router.beforeEach((to, from, next) => {
  const token = localStorage.getItem("token");
  const role = localStorage.getItem("role");
  if (to.path === "/login" || to.path === "/register") {
    return next();
  }
  if (!token) {
    return next("/login");
  }
  if (to.meta && to.meta.role && to.meta.role !== role) {
    if (role === "ADMIN") return next("/admin");
    if (role === "NURSE") return next("/nurse");
    if (role === "FAMILY") return next("/family");
  }
  return next();
});

export default router;
