/**
 * modules/core/session.js — Sesión del usuario actual (sessionStorage).
 */
(function (g) {
    g.modules = g.modules || {};
    g.modules.core = g.modules.core || {};

    var KEY_USER = 'grid_auth_user_json';

    g.modules.core.authSession = {
        setUser: function (user) {
            try {
                var slim = {
                    id: user.id,
                    email: user.email,
                    phone: user.phone,
                    role: user.role,
                    metadata: user.metadata || {},
                };
                sessionStorage.setItem(KEY_USER, JSON.stringify(slim));
            } catch (_) {}
        },

        getUser: function () {
            try {
                var raw = sessionStorage.getItem(KEY_USER);
                if (!raw) return null;
                return JSON.parse(raw);
            } catch (_) {
                return null;
            }
        },

        clear: function () {
            try {
                sessionStorage.removeItem(KEY_USER);
            } catch (_) {}
        },

        isAuthenticated: function () {
            return !!this.getUser();
        },
    };
})(typeof window !== 'undefined' ? window : this);
