import AppLayout from '@/layout/AppLayout.vue';
import { createRouter, createWebHistory } from 'vue-router';
import { clearSuperAuthToken, hasSuperAuthToken, refreshSuperToken } from '@/service/auth';

const router = createRouter({
    history: createWebHistory('/super/'),
    routes: [
        {
            path: '/',
            component: AppLayout,
            meta: { requiresAuth: true },
            children: [
                {
                    path: '/',
                    name: 'dashboard',
                    component: () => import('@/views/Dashboard.vue')
                },
                {
                    path: '/uikit/formlayout',
                    name: 'formlayout',
                    component: () => import('@/views/uikit/FormLayout.vue')
                },
                {
                    path: '/uikit/input',
                    name: 'input',
                    component: () => import('@/views/uikit/InputDoc.vue')
                },
                {
                    path: '/uikit/button',
                    name: 'button',
                    component: () => import('@/views/uikit/ButtonDoc.vue')
                },
                {
                    path: '/uikit/table',
                    name: 'table',
                    component: () => import('@/views/uikit/TableDoc.vue')
                },
                {
                    path: '/uikit/list',
                    name: 'list',
                    component: () => import('@/views/uikit/ListDoc.vue')
                },
                {
                    path: '/uikit/tree',
                    name: 'tree',
                    component: () => import('@/views/uikit/TreeDoc.vue')
                },
                {
                    path: '/uikit/panel',
                    name: 'panel',
                    component: () => import('@/views/uikit/PanelsDoc.vue')
                },

                {
                    path: '/uikit/overlay',
                    name: 'overlay',
                    component: () => import('@/views/uikit/OverlayDoc.vue')
                },
                {
                    path: '/uikit/media',
                    name: 'media',
                    component: () => import('@/views/uikit/MediaDoc.vue')
                },
                {
                    path: '/uikit/message',
                    name: 'message',
                    component: () => import('@/views/uikit/MessagesDoc.vue')
                },
                {
                    path: '/uikit/file',
                    name: 'file',
                    component: () => import('@/views/uikit/FileDoc.vue')
                },
                {
                    path: '/uikit/menu',
                    name: 'menu',
                    component: () => import('@/views/uikit/MenuDoc.vue')
                },
                {
                    path: '/uikit/charts',
                    name: 'charts',
                    component: () => import('@/views/uikit/ChartDoc.vue')
                },
                {
                    path: '/uikit/misc',
                    name: 'misc',
                    component: () => import('@/views/uikit/MiscDoc.vue')
                },
                {
                    path: '/uikit/timeline',
                    name: 'timeline',
                    component: () => import('@/views/uikit/TimelineDoc.vue')
                },
                {
                    path: '/blocks',
                    name: 'blocks',
                    meta: {
                        breadcrumb: ['Prime Blocks', 'Free Blocks']
                    },
                    component: () => import('@/views/utilities/Blocks.vue')
                },
                {
                    path: '/pages/empty',
                    name: 'empty',
                    component: () => import('@/views/pages/Empty.vue')
                },
                {
                    path: '/pages/crud',
                    name: 'crud',
                    component: () => import('@/views/pages/Crud.vue')
                },
                {
                    path: '/documentation',
                    name: 'documentation',
                    component: () => import('@/views/pages/Documentation.vue')
                },
                {
                    path: '/superadmin/tenants',
                    name: 'superadmin-tenants',
                    component: () => import('@/views/superadmin/Tenants.vue')
                },
                {
                    path: '/superadmin/tenants/:tenantID',
                    name: 'superadmin-tenant-detail',
                    component: () => import('@/views/superadmin/TenantDetail.vue')
                },
                {
                    path: '/superadmin/users',
                    name: 'superadmin-users',
                    component: () => import('@/views/superadmin/Users.vue')
                },
                {
                    path: '/superadmin/users/:userID',
                    name: 'superadmin-user-detail',
                    component: () => import('@/views/superadmin/UserDetail.vue')
                },
                {
                    path: '/superadmin/orders',
                    name: 'superadmin-orders',
                    component: () => import('@/views/superadmin/Orders.vue')
                },
                {
                    path: '/superadmin/contents',
                    name: 'superadmin-contents',
                    component: () => import('@/views/superadmin/Contents.vue')
                },
                {
                    path: '/superadmin/creators',
                    name: 'superadmin-creators',
                    component: () => import('@/views/superadmin/Creators.vue')
                },
                {
                    path: '/superadmin/coupons',
                    name: 'superadmin-coupons',
                    component: () => import('@/views/superadmin/Coupons.vue')
                },
                {
                    path: '/superadmin/finance',
                    name: 'superadmin-finance',
                    component: () => import('@/views/superadmin/Finance.vue')
                },
                {
                    path: '/superadmin/reports',
                    name: 'superadmin-reports',
                    component: () => import('@/views/superadmin/Reports.vue')
                },
                {
                    path: '/superadmin/assets',
                    name: 'superadmin-assets',
                    component: () => import('@/views/superadmin/Assets.vue')
                },
                {
                    path: '/superadmin/notifications',
                    name: 'superadmin-notifications',
                    component: () => import('@/views/superadmin/Notifications.vue')
                },
                {
                    path: '/superadmin/orders/:orderID',
                    name: 'superadmin-order-detail',
                    component: () => import('@/views/superadmin/OrderDetail.vue')
                }
            ]
        },
        {
            path: '/landing',
            name: 'landing',
            component: () => import('@/views/pages/Landing.vue')
        },
        {
            path: '/pages/notfound',
            name: 'notfound',
            component: () => import('@/views/pages/NotFound.vue')
        },

        {
            path: '/auth/login',
            name: 'login',
            component: () => import('@/views/pages/auth/Login.vue')
        },
        {
            path: '/auth/access',
            name: 'accessDenied',
            component: () => import('@/views/pages/auth/Access.vue')
        },
        {
            path: '/auth/error',
            name: 'error',
            component: () => import('@/views/pages/auth/Error.vue')
        }
    ]
});

let tokenValidated = false;
let tokenValidationPromise = null;

router.beforeEach(async (to) => {
    if (to.meta?.requiresAuth !== true) return true;

    const isAuthed = hasSuperAuthToken();
    if (!isAuthed) {
        return {
            name: 'login',
            query: { redirect: to.fullPath }
        };
    }

    if (!tokenValidated) {
        tokenValidationPromise ??= refreshSuperToken();
        try {
            await tokenValidationPromise;
            tokenValidated = true;
        } catch {
            clearSuperAuthToken();
            tokenValidated = false;
            tokenValidationPromise = null;
            return { name: 'login', query: { redirect: to.fullPath } };
        }
    }

    return true;
});

export default router;
