// role_bindings.js — Attach role-specific UI behaviors on legacy PHP pages

(function() {

    function init() {
        try {
            // Inject PWA header/nav if container placeholders exist
            if (document.getElementById('pwa_header_injected')) {
                if (window.renderHeader) window.renderHeader();
            }
            if (document.getElementById('pwa_nav_injected')) {
                if (window.buildNav) window.buildNav();
            }

            // Read role from localStorage
            const role = (localStorage.getItem('role') || '').toLowerCase();

            // Add SolarDoot quick-add FAB
            if (role === 'solardoot') addSolarFab();

        } catch (err) {
            console.error("role_bindings error:", err);
        }
    }

    function addSolarFab() {
        if (document.getElementById('pwa_fab_solardoot')) return;

        const fab = document.createElement('button');
        fab.id = 'pwa_fab_solardoot';
        fab.innerText = '+';
        fab.style.position = 'fixed';
        fab.style.bottom = '76px';
        fab.style.right = '16px';
        fab.style.width = '56px';
        fab.style.height = '56px';
        fab.style.borderRadius = '50%';
        fab.style.background = '#0a6ebd';
        fab.style.color = '#fff';
        fab.style.fontSize = '28px';
        fab.style.border = 'none';
        fab.style.boxShadow = '0 6px 10px rgba(0,0,0,0.25)';
        fab.style.zIndex = '9999';

        fab.addEventListener('click', function() {
            // SPA-compatible navigation
            location.href = '/pwa/index.html#customers';
        });

        document.body.appendChild(fab);
    }

    document.addEventListener('DOMContentLoaded', init);
})();
