import { createRouter, createWebHistory } from "vue-router";
import authRoutes from "./modules/authRoutes";
import settingRoutes from "./modules/settingRoutes";
import itemRoutes from "./modules/itemRoutes";
import frontendRoutes from "./modules/frontendRoutes";
import { useAuthStore } from "../stores/auth";
import DashboardComponent from "../components/admin/dashboard/DashboardComponent.vue";
import administratorRoutes from "./modules/administratorRoutes";
import customerRoutes from "./modules/customerRoutes";
import employeeRoutes from "./modules/employeeRoutes";
import deliveryBoyRoutes from "./modules/deliveryBoyRoutes";
import creditBalanceReportRoutes from "./modules/creditBalanceReportRoutes";
import itemsReportRoutes from "./modules/itemsReportRoutes";
import salesReportRoutes from "./modules/salesReportRoutes";
import transactionRoutes from "./modules/transactionRoutes";
import pushNotificationRoutes from "./modules/pushNotificationRoutes";
import subscriberRoutes from "./modules/subscriberRoutes";
import allOrderRoutes from "./modules/allOrderRoutes";
import NotFoundComponent from "../components/frontend/otherPage/NotFoundComponent.vue";
import ExceptionComponent from "../components/frontend/otherPage/ExceptionComponent.vue";
import posRoutes from "./modules/posRoutes";
import profileRoutes from "./modules/profileRoutes";
import messageRoutes from "./modules/messageRoutes";

const baseRoutes = [
    {
        path: "/",
        redirect: { name: "frontend.home" },
        name: "root"
    },
    {
        path: "/:pathMatch(.*)*",
        name: "route.notFound",
        component: NotFoundComponent,
        meta: {
            isFrontend: true,
        },
    },
    {
        path: "/exception",
        name: "route.exception",
        component: ExceptionComponent,
    },
    {
        path: "/admin/dashboard",
        component: DashboardComponent,
        name: "admin.dashboard",
        meta: {
            isFrontend: false,
            auth: true,
            permissionUrl: "dashboard",
            breadcrumb: "dashboard",
        },
    },
];


export const routes = baseRoutes.concat(
    authRoutes,
    frontendRoutes,
    settingRoutes,
    itemRoutes,
    administratorRoutes,
    customerRoutes,
    employeeRoutes,
    deliveryBoyRoutes,
    creditBalanceReportRoutes,
    itemsReportRoutes,
    salesReportRoutes,
    transactionRoutes,
    pushNotificationRoutes,
    subscriberRoutes,
    allOrderRoutes,
    posRoutes,
    profileRoutes,
    messageRoutes


);

const router = createRouter({
    linkActiveClass: "active",
    mode: 'history',
    history: createWebHistory(),
    routes,
    scrollBehavior() {
        return { left: 0, top: 0 }
    }
});

router.beforeEach((to, from, next) => {
    const authStore = useAuthStore();
    if (to.meta.auth === true) {
        if (!authStore.authStatus) {
            next({ name: "auth.login" });
        } else {
            if (to.meta.isFrontend === false) {
                if (to.meta.access === false) {
                    next({
                        name: "route.exception",
                    });
                } else {
                    next();
                }
            } else {
                next();
            }
        }
    } else if ((to.name === "auth.login" || to.name === "auth.signupPhone" || to.name === "auth.guestLogin") && authStore.authStatus) {
        next({ name: "frontend.home" });
    } else {
        next();
    }
});
export default router;