import {createRouter, createWebHistory} from 'vue-router';
import Calculations from "@/pages/Calculations.vue";
import Reporting from "@/pages/Reporting.vue";
import Config from "@/pages/Config.vue";
import CalculationSingleEdit from "@/pages/CalculationSingleEdit.vue";
import CalculationMassEdit from "@/pages/CalculationMassEdit.vue";
import CalculationAssistant from "@/pages/CalculationAssistant.vue";
import CalculationDump from "@/components/layout/dev/CalculationDump.vue";
import DevPage from "@/pages/DevPage.vue";
import {useActiveUserStore} from "@/store/activeuser.js";
import NotAuthorized from "@/pages/NotAuthorized.vue";

const router = createRouter({
    history: createWebHistory(),
    routes: [
        {
            path: '/not-authorized',
            component: NotAuthorized,
            name: 'not-authorized',
        },
        {
            path: '/',
            redirect: '/calculations',
            name: 'calculation-list',
        },
        {
            path: '/assistant',
            component: CalculationAssistant,
            name: 'assistant',
             beforeEnter: async (to, from) => {

                const userStore = useActiveUserStore();
                await userStore.loadIfRequired();

                if (userStore.allowCalculation) {
                    return true;
                }
                return {name: 'not-authorized'};
            },
        },
        {
            path: '/calculations',
            component: Calculations,
            name: 'home',
            beforeEnter: async (to, from) => {
                const userStore = useActiveUserStore();
                await userStore.loadIfRequired();

                if (userStore.allowCalculation) {
                    return true;
                }
                return {name: 'not-authorized'};
            },
        },
        {
            path: '/edit/:id',
            component: CalculationSingleEdit,
            name: 'edit',
            beforeEnter: async (to, from) => {
                const userStore = useActiveUserStore();
                await userStore.load();

                if (userStore.allowCalculation) {
                    return true;
                }
                return {name: 'not-authorized'};
            },
        },
        {
            path: '/bulk/:ids',
            component: CalculationMassEdit,
            name: 'bulk',
            beforeEnter: async (to, from) => {
                const userStore = useActiveUserStore();
                await userStore.load();

                if (userStore.allowCalculation) {
                    return true;
                }
                return {name: 'not-authorized'};
            },
        },
        {
            path: '/bulk/:ids/edit/:id',
            component: CalculationSingleEdit,
            name: 'bulk-single-edit',
            beforeEnter: async (to, from) => {
                const userStore = useActiveUserStore();
                await userStore.load();

                if (userStore.allowCalculation) {
                    return true;
                }
                return {name: 'not-authorized'};
            },
        },
        {
            path: '/reports',
            component: Reporting,
            beforeEnter: async (to, from) => {
                const userStore = useActiveUserStore();
                await userStore.load();

                if (userStore.allowReporting) {
                    return true;
                }
                return {name: 'not-authorized'};
            },
        },
        {
            path: '/config',
            component: Config,
            beforeEnter: async (to, from) => {
                const userStore = useActiveUserStore();
                await userStore.load();

                if (userStore.allowConfiguration) {
                    return true;
                }
                return {name: 'not-authorized'};
            },
        },
        {
            path: '/dev/dump/:id',
            component: CalculationDump,
            name: 'dev-calculation-dump'

        },
        {
            path: '/dev',
            component: DevPage,
            name: 'dev-page',
        },
        {
            path: '/:pathMatch(.*)*',
            redirect: '/calculations'
        }
    ]
})

export default router;