import {defineStore} from 'pinia'
import {config} from '@/config'
import performRequest, {getCsrfToken} from "@/backend.js";
import logger from "@/logger.js";


export const useActiveUserStore = defineStore('activeUser', {
    state: () => {
        return {
            user: null,
            lastActivity: Date.now(),
            sessionRefreshInterval: null
        }
    },
    getters: {
        allowCalculation(state) {
            if (state.user === null)
                return false;
            return state.user.groups?.includes("super") || state.user.groups?.includes("calculation");
        },
        allowConfiguration(state) {
            if (state.user === null)
                return false;
            return state.user.groups?.includes("super") || state.user.groups?.includes("freight") || state.user.groups?.includes("packaging") || state.user.groups?.includes("service") || state.user.groups?.includes("material");
        },
        allowReporting(state) {
            if (state.user === null)
                return false;
            return state.user.groups?.includes("super") || state.user.groups?.includes("freight") || state.user.groups?.includes("packaging") || state.user.groups?.includes("material") || state.user.groups?.includes("basic") || state.user.groups?.includes("calculation");
        },
        isSuper(state) {
            if (state.user === null)
                return false;
            return state.user.groups?.includes("super");
        },
        isService(state) {
            if (state.user === null)
                return false;
            return state.user.groups?.includes("service");
        },
        isRightManagement(state) {
            if (state.user === null)
                return false;
            return state.user.groups?.includes("right-management");
        },
        isPackaging(state) {
            if (state.user === null)
                return false;
            return state.user.groups?.includes("packaging");
        },
        isFreight(state) {
            if (state.user === null)
                return false;
            return state.user.groups?.includes("freight");
        },
        isMaterial(state) {
            if (state.user === null)
                return false;
            return state.user.groups?.includes("material");
        },
        allowRates(state) {
            if (state.user === null)
                return false;
            return state.user.groups?.includes("super") || state.user.groups?.includes("freight");
        }

    },
    actions: {
        trackActivity() {
            this.lastActivity = Date.now();
        },
        setupSessionRefresh() {
            if (this.sessionRefreshInterval) {
                clearInterval(this.sessionRefreshInterval);
            }
            this.sessionRefreshInterval = setInterval(async () => {
                const timeSinceActivity = Date.now() - this.lastActivity;

                if (timeSinceActivity < (10 * 60 * 1000)) {
                    await this.refreshSession();
                }
            }, 2 * 60 * 1000);
        },
        async refreshSession() {
            try {
                await this.load();
                logger.log('Session refreshed');
            } catch (e) {
                logger.error('Session refresh failed', e);
            }
        },
        async loadIfRequired() {
            if (this.user === null)
                await this.load();
        },
        async load() {
            const resp = await performRequest(this, "GET", `${config.backendUrl}/active-user`, null, true);
            this.user = resp.data;
        }
    }
});


export function setupSessionRefresh() {
    useActiveUserStore().setupSessionRefresh();
}

// const stopSessionRefresh = () => {
//     useActiveUserStore().stopSessionRefresh
//     if (sessionRefreshInterval) {
//         clearInterval(sessionRefreshInterval);
//     }
// }
