// import { setupLayouts } from 'virtual:generated-layouts';
import { createRouter, createWebHistory } from "vue-router";
import { useStore } from "vuex";

const router = createRouter({
    history: createWebHistory(),
    routes: [
        {
            path: "/home",
            component: () => import("../pages/home.vue"),
            alias: "/",
        },
        {
            path: "/",
            component: () => import("../layouts/agentCallDefault.vue"),
            children: [
                {
                    path: "/provider/patient-profile",
                    component: () => import("../pages/PatientProfile.vue"),
                    meta: {
                        requiresAuth: true,
                        requiresRole: "agent",
                    },
                },
                {
                    path: "/provider/telehealth",
                    component: () => import("../pages/conference.vue"),
                    meta: {
                        requiresAuth: true,
                        requiresRole: "agent",
                    },
                },
                {
                    path: "/provider/question-categories",
                    component: () => import("../pages/question-categories.vue"),
                    meta: {
                        requiresAuth: true,
                        requiresRole: "agent",
                    },
                },
                {
                    path: "/provider/prescriptions",
                    component: () => import("../pages/PrescriptionPatient.vue"),
                    meta: {
                        requiresAuth: true,
                        requiresRole: "agent",
                    },
                },
                {
                    path: "/provider/lab-kit-order",
                    component: () =>
                        import("../pages/patient-lab-kit-order.vue"),
                    meta: {
                        requiresAuth: true,
                        requiresRole: "agent",
                    },
                },

                {
                    path: "/provider/notes",
                    component: () => import("../pages/NotesPatient.vue"),
                    meta: {
                        requiresAuth: true,
                        requiresRole: "agent",
                    },
                },

                {
                    path: "provider/questionere/:categoryLink",
                    component: () => import("../pages/questionere.vue"),
                    meta: {
                        requiresAuth: true,
                        requiresRole: "agent",
                    },
                },
            ],
        },
        {
            path: "/",
            component: () => import("../layouts/default.vue"),
            children: [
                {
                    path: "overview",
                    component: () => import("../pages/overview.vue"),
                    meta: {
                        requiresAuth: true,
                    },
                },

                {
                    path: "queue",
                    component: () => import("../pages/queue.vue"),
                    meta: {
                        requiresAuth: true,
                    },
                },
                {
                    path: "/questionere/:categoryLink",
                    component: () => import("../pages/questionere.vue"),
                    meta: {
                        requiresAuth: true,
                    },
                },
                {
                    path: "/provider/dashboard",
                    component: () => import("../pages/provider/dashboard.vue"),
                    meta: {
                        requiresAuth: true,
                        requiresRole: "agent",
                    },
                },
                {
                    path: "/provider/meeting-history",
                    component: () =>
                        import("../pages/provider/meeting-history.vue"),
                    meta: {
                        requiresAuth: true,
                        requiresRole: "agent",
                    },
                },
                {
                    path: "/provider/detail-history/:patient_id/:id",
                    component: () =>
                        import("../pages/provider/detail-history.vue"),
                    meta: {
                        requiresAuth: true,
                        requiresRole: "agent",
                    },
                },
                {
                    path: "testfile",
                    component: () => import("../pages/testfile.vue"),
                    meta: {
                        requiresAuth: true,
                    },
                },
                {
                    path: "/provider/appointments",
                    component: () => import("../pages/appointments.vue"),
                    meta: {
                        requiresAuth: true,
                        requiresRole: "agent",
                    },
                },
                {
                    path: "/provider/queue-users",
                    component: () => import("../pages/queue-users.vue"),
                    meta: {
                        requiresAuth: true,
                        requiresRole: "agent",
                    },
                },
                {
                    path: "/provider/join-meeting",
                    component: () => import("../pages/join-meeting.vue"),
                    meta: {
                        requiresAuth: true,
                        requiresRole: "agent",
                    },
                },
                {
                    path: "/provider/telehealth",
                    component: () => import("../pages/conference.vue"),
                    meta: {
                        requiresAuth: true,
                        requiresRole: "agent",
                    },
                },

                {
                    path: "book-slot",
                    component: () => import("../pages/book-slot.vue"),
                    meta: {
                        requiresAuth: true,
                        requiresRole: "agent",
                    },
                },
                {
                    path: "/provider/doctor-appiontments",
                    component: () => import("../pages/doctor-appiontment.vue"),
                    meta: {
                        requiresAuth: true,

                        // requiresRole:'agent'
                    },
                },
                {
                    path: "/provider/appiontments-detail",
                    component: () =>
                        import("../pages/doctor-appiontment-detail.vue"),
                    meta: {
                        requiresAuth: true,

                        // requiresRole:'agent'
                    },
                },
                {
                    path: "/doctor-appiontments",
                    component: () => import("../pages/doctor-appiontment.vue"),
                    meta: {
                        requiresAuth: true,

                        // requiresRole:'agent'
                    },
                },
                {
                    path: "/doctor-appiontments",
                    component: () => import("../pages/doctor-appiontment.vue"),
                    meta: {
                        requiresAuth: true,

                        // requiresRole:'agent'
                    },
                },
                {
                    path: "/schedules",
                    component: () => import("../pages/patient/schedules.vue"),
                    meta: {
                        requiresAuth: true,
                        requiresRole: "patient",
                    },
                },
                {
                    path: "/complete",
                    component: () =>
                        import("../pages/patient/complete-appiontments.vue"),
                    meta: {
                        requiresAuth: true,
                        requiresRole: "patient",
                    },
                },
                {
                    path: "/patient/complete-appintment-detail/:appiontment_id",
                    component: () =>
                        import(
                            "../pages/patient/complete-appintment-detail.vue"
                        ),
                    meta: {
                        requiresAuth: true,
                    },
                },

                {
                    path: "/prescriptions",
                    component: () =>
                        import("../pages/patient/prescriptions-history.vue"),
                    meta: {
                        requiresAuth: true,

                        // requiresRole:'agent'
                    },
                },

                {
                    path: "/patient/prescription-history/:appiontment_id",
                    component: () =>
                        import("../pages/patient/prescription-detail.vue"),
                    meta: {
                        requiresAuth: true,
                    },
                },

                {
                    path: "/profile",
                    component: () =>
                        import("../pages/patient/AccountSettingsAccount.vue"),
                    meta: {
                        requiresAuth: true,
                    },
                },
                {
                    path: "/change-password",
                    component: () =>
                        import("../pages/patient/AccountSettingsSecurity.vue"),
                    meta: {
                        requiresAuth: true,
                    },
                },
                {
                    path: "/order-detail/:id",
                    name: "order-detail",
                    component: () =>
                        import("../pages/patient/main-orders-detail-tabs.vue"),
                    meta: {
                        requiresAuth: true,
                        activeParent: "patient-order-list",
                    },
                },
                {
                    path: "/subscriptions",
                    component: () =>
                        import("../pages/patient/subscription.vue"),
                    meta: {
                        requiresAuth: true,
                    },
                },
                {
                    path: "/subscriptions-detail/:id",
                    component: () =>
                        import("../pages/patient/subscriptionDetail.vue"),
                    meta: {
                        requiresAuth: true,
                    },
                },

                {
                    path: "/provider/patient-labkit-detail",
                    component: () =>
                        import("../pages/patient-labkit-detail.vue"),
                    meta: {
                        requiresAuth: true,
                        requiresRole: "agent",
                    },
                },
                {
                    path: "/provider/order-detail/:id",
                    name: "provider-order-detail",
                    component: () =>
                        import("../pages/provider/main-orders-detail-tabs.vue"),
                    meta: {
                        requiresAuth: true,
                        requiresRole: "agent",
                    },
                },

                {
                    path: "/provider/orders-list-new",
                    name: "provider-orders-list-new",
                    component: () =>
                        import("../pages/provider/orders-list-new.vue"),
                    meta: {
                        requiresAuth: true,
                        requiresRole: "agent",
                    },
                },
                {
                    path: "/provider/orders-list",
                    name: "provider-order-list",
                    component: () =>
                        import("../pages/provider/orders-list-new.vue"),
                    meta: {
                        requiresAuth: true,
                        requiresRole: "agent",
                    },
                },
                {
                    path: "/provider/patient-profile-detail/:id",
                    name: "provider-patient-profile-detail",
                    component: () =>
                        import(
                            "../pages/provider/patient-profile/patient-profile.vue"
                        ),
                    meta: {
                        requiresAuth: true,
                        requiresRole: "agent",
                        layout: "default",
                    },
                },
                {
                    path: "/provider/chat",
                    name: "provider-chat",
                    component: () => import("../pages/provider/chat.vue"),
                    meta: {
                        requiresAuth: true,
                        requiresRole: "agent",
                    },
                },
                {
                    path: "profile",
                    component: () => import("../pages/profile.vue"),
                },
                {
                    path: "update",
                    component: () =>
                        import("../pages/update-medical-profile.vue"),
                },
                {
                    path: "category",
                    component: () => import("../pages/category.vue"),
                },
                {
                    path: "preceptions",
                    component: () => import("../pages/preceptions.vue"),
                },
                {
                    path: "category-group",
                    component: () => import("../pages/category-group.vue"),
                },
                {
                    path: "/category-question/:categoryLink",
                    component: () => import("../pages/category-questions.vue"),
                    props: true,
                },

                {
                    path: "treatment-plan",
                    component: () => import("../pages/treatment-plan.vue"),
                },

                {
                    path: "complete-patient-details",
                    component: () =>
                        import("../pages/complete-patient-details.vue"),
                },
                {
                    path: "membership",
                    component: () => import("../pages/membership.vue"),
                },
                {
                    path: "labs",
                    component: () => import("../pages/labs.vue"),
                },
                {
                    path: "orders-list",
                    name: "patient-order-list",
                    component: () => import("../pages/patient/orders-list.vue"),
                    meta: {
                        requiresAuth: true,
                    },
                },
                {
                    path: "orders-detail1/:id",
                    name: "order-detail1",
                    component: () =>
                        import("../pages/patient/orders-detail.vue"),
                    meta: {
                        requiresAuth: true,
                        activeParent: "patient-order-list",
                    },
                },

                {
                    path: "lab-kits",
                    component: () => import("../pages/lab-kits.vue"),
                },
                {
                    path: "account-settings",
                    component: () => import("../pages/account-settings.vue"),
                },
                {
                    path: "contactus",
                    component: () => import("../pages/contactus.vue"),
                },
                {
                    path: "typography",
                    component: () => import("../pages/typography.vue"),
                },
                {
                    path: "icons",
                    component: () => import("../pages/icons.vue"),
                },
                {
                    path: "cards",
                    component: () => import("../pages/cards.vue"),
                },

                {
                    path: "tables",
                    component: () => import("../pages/tables.vue"),
                },
                {
                    path: "form-layouts",
                    component: () => import("../pages/form-layouts.vue"),
                },
            ],
        },

        {
            path: "/",
            component: () => import("../layouts/blank.vue"),
            children: [
                {
                    path: "login",
                    component: () => import("../pages/login.vue"),
                },
                {
                    path: "/forgot",
                    component: () => import("../pages/forgot.vue"),
                },
                // {
                //     path: '/reset-password?token=:token',
                //     component: () => import("../pages/reset.vue"),
                // },
                {
                    path: "/password-reset",
                    name: "ResetPassword",
                    component: () => import("../pages/reset.vue"),
                    props: (route) => ({ token: route.query.token }),
                },
                {
                    path: "/home",
                    component: () => import("../pages/home.vue"),
                    alias: "/",
                },
                {
                    path: "/terms-and-conditions",
                    component: () => import("../pages/terms-conditions.vue"),
                },
                {
                    path: "/privacy-policy",
                    component: () => import("../pages/privacy-policy.vue"),
                },
                {
                    path: "/refund-policy",
                    component: () => import("../pages/refund-policy.vue"),
                },
                {
                    path: "provider/login",
                    component: () => import("../pages/agent-login.vue"),
                },
                {
                    path: "provider/register",
                    component: () => import("../pages/agent-register.vue"),
                },
                {
                    path: "provider/thank-you",
                    component: () =>
                        import("../pages/success-provider-register.vue"),
                },

                {
                    path: "not-authorized",
                    component: () => import("../pages/not-authorized.vue"),
                },

                {
                    path: "pre-register",
                    component: () => import("../pages/pre-register.vue"),
                },

                {
                    path: "register",
                    component: () => import("../pages/register.vue"),
                },
                {
                    path: "go/checkout",
                    component: () => import("../pages/go-checkout.vue"),
                    query: {
                        cart: "cart",
                    },
                },
                {
                    path: "checkout",
                    component: () => import("../pages/checkout.vue"),
                },
                {
                    path: "form-checkout",
                    component: () => import("../pages/form-checkout.vue"),
                },
                {
                    path: "form-checkout-2",
                    component: () => import("../pages/form-checkout-2.vue"),
                },
                {
                    path: "plans",
                    component: () => import("../pages/plans.vue"),
                    query: {
                        slug: "slug",
                        id: "id",
                    },
                },

                {
                    path: "book-appointment",
                    component: () => import("../pages/book-appointment.vue"),
                },
                {
                    path: "invoice",
                    component: () => import("../pages/invoice.vue"),
                },
                {
                    path: "review-appointment",
                    component: () => import("../pages/review-appointment.vue"),
                },
                {
                    path: "additional-information",
                    component: () =>
                        import("../pages/additional-information.vue"),
                },
                {
                    path: "shipping-information",
                    component: () =>
                        import("../pages/shipping-information.vue"),
                },
                {
                    path: "process-payment",
                    component: () => import("../pages/process-payment.vue"),
                },
                {
                    path: "register-assessment",
                    component: () =>
                        import(
                            "../pages/register-assessment/register-assessment.vue"
                        ),
                },

                {
                    path: "trt",
                    component: () => import("../pages/trt/get-started-trt.vue"),
                },
                {
                    path: "our-story",
                    component: () => import("../pages/nav/OurStory.vue"),
                },
                {
                    path: "our-providers",
                    component: () => import("../pages/nav/OurProviders.vue"),
                },
                {
                    path: "our-protocols",
                    component: () => import("../pages/nav/OurProtocols.vue"),
                },
                {
                    path: "health-guid",
                    component: () => import("../pages/nav/HealthGuid.vue"),
                },

                // {
                //   path: 'queue',
                //   component: () =>
                //     import ('../pages/queue.vue'),
                //   meta: {
                //     requiresAuth: true,
                //     requiresRole: 'patient',
                //   },

                // },
                {
                    path: "thankyou",
                    component: () => import("../pages/thankyou.vue"),
                },
                {
                    path: "meeting",
                    component: () => import("../pages/meeting.vue"),
                    meta: {
                        requiresAuth: true,
                        requiresRole: "patient",
                    },
                },
                {
                    path: "telehealth",
                    component: () => import("../pages/call.vue"),
                    meta: {
                        requiresAuth: true,
                        requiresRole: "patient",
                    },
                },
                {
                    path: "/:pathMatch(.*)*",
                    component: () => import("../pages/[...all].vue"),
                },
            ],
        },

        // ...setupLayouts(routes),
    ],
});

const auth = {
    isAuthenticated() {
        return localStorage.getItem("access_token") !== null;
    },
    getUserRole() {
        return localStorage.getItem("user_role");
    },
    isLogin() {
        return localStorage.getItem("isLogin");
    },
    profileStatus() {
        return localStorage.getItem("profileCompleted");
    },
    currentPage() {
        const currentPageValue = localStorage.getItem("currentPage");
        console.log("currentPageValue", currentPageValue);
        return currentPageValue;
    },
};

router.beforeEach((to, from, next) => {
    const loginCheck = auth.isLogin();
    const userRole = auth.getUserRole();
    const profileStatusCheck = auth.profileStatus();
    const currentPageCheck = auth.currentPage();

    // If the route requires authentication or the user is authenticated
    if (
        to.matched.some(
            (record) => record.meta.requiresAuth || auth.isAuthenticated()
        )
    ) {
        if (!auth.isAuthenticated()) {
            console.log("for login", to.path);
            // If the user is not authenticated, redirect to the login page
            next("/login");
        } else {
            if (to.meta.requiresRole && userRole !== to.meta.requiresRole) {
                // If the route requires a specific role and the user doesn't have it, redirect to the not-authorized page
                next("/not-authorized");
            } else if (to.path === "/login" && userRole === "patient") {
                console.log("q");
                // If the user is a patient and tries to access the login page, redirect to the queue page
                next("/queue");
            } else if (
                userRole === "patient" &&
                profileStatusCheck === "0" &&
                to.path !== "/complete-patient-details" &&
                to.path !== "/register-assessment"
            ) {
                // If the user is a patient with incomplete profile and tries to access a page other than complete-patient-details, redirect to it
                next("/complete-patient-details");
            }
            // else if(userRole === 'patient'
            // && currentPageCheck === 'process-payment'
            // && to.path === '/thankyou'){

            //   console.log('/'+currentPageCheck,to.path)
            //   next()

            // }
            else if (
                userRole === "patient" &&
                currentPageCheck &&
                currentPageCheck !== "plans" &&
                currentPageCheck !== "register" &&
                currentPageCheck !== "pre-register" &&
                currentPageCheck !== "thankyou" &&
                // && to.path !== '/plans'
                // && to.path !== '/register'
                to.path !== "/book-appointment" &&
                to.path !== "/review-appointment" &&
                to.path !== "/shipping-information" &&
                to.path !== "/checkout" &&
                to.path !== "/additional-information" &&
                to.path !== "/process-payment" &&
                to.path !== "/thankyou"
            ) {
                console.log("here >><<" + currentPageCheck, to.path);
                const store = useStore();
                store.dispatch("updateShowStartOverPupup", true);
                next(currentPageCheck);
            } else {
                next();
            }
        }
    } else {
        // If the route doesn't require authentication, proceed with the navigation
        next();
    }
});

export default router;
