/* ================================================================
   Rapha-Bethel BNPS — main.js
   Kept minimal: every byte costs money on 2G/3G in Cameroon.
================================================================ */
(function () {
    'use strict';

    /* ── SIDEBAR TOGGLE ──────────────────────────────────────── */
    const body      = document.body;
    const overlay   = document.getElementById('sidebarOverlay');
    const toggle    = document.getElementById('sidebarToggle');
    const mobileBtn = document.getElementById('mobileMenuBtn');

    function openSidebar()  { body.classList.add('sidebar-open'); }
    function closeSidebar() { body.classList.remove('sidebar-open'); }

    if (toggle)    toggle.addEventListener('click', () => body.classList.toggle('sidebar-open'));
    if (mobileBtn) mobileBtn.addEventListener('click', openSidebar);
    if (overlay)   overlay.addEventListener('click', closeSidebar);

    /* Close on ESC */
    document.addEventListener('keydown', e => { if (e.key === 'Escape') closeSidebar(); });

    /* ── AUTO-DISMISS ALERTS (4 s) ───────────────────────────── */
    document.querySelectorAll('.js-auto-dismiss').forEach(el => {
        setTimeout(() => {
            el.style.transition = 'opacity .4s';
            el.style.opacity = '0';
            setTimeout(() => el.remove(), 400);
        }, 4000);
    });

    /* ── OFFLINE DETECTION ───────────────────────────────────── */
    const bar = document.getElementById('rb-offline-bar');
    function syncOnline() {
        if (bar) bar.style.display = navigator.onLine ? 'none' : 'block';
    }
    window.addEventListener('online',  syncOnline);
    window.addEventListener('offline', syncOnline);
    syncOnline();

    /* ── BOOTSTRAP FORM STYLING ──────────────────────────────── */
    /* Add Bootstrap classes to raw Django form widgets */
    document.querySelectorAll('form input:not([type=hidden]):not([type=checkbox]):not([type=radio]):not(.form-control):not(.form-check-input), form textarea:not(.form-control)').forEach(el => {
        el.classList.add('form-control');
    });
    document.querySelectorAll('form select:not(.form-select)').forEach(el => {
        el.classList.add('form-select');
    });

    /* ── TABLE RESPONSIVENESS HINT ───────────────────────────── */
    /* Wrap bare tables in a responsive div if not already */
    document.querySelectorAll('.card table:not(.no-wrap)').forEach(table => {
        if (!table.parentElement.classList.contains('table-responsive')) {
            const wrapper = document.createElement('div');
            wrapper.className = 'table-responsive';
            table.parentNode.insertBefore(wrapper, table);
            wrapper.appendChild(table);
        }
    });

    /* ── DATE FIELD ENHANCEMENTS ─────────────────────────────── */
    /* Set max date on all date inputs to prevent future dates where sensible */
    const today = new Date().toISOString().split('T')[0];
    document.querySelectorAll('input[type=date][data-max-today]').forEach(el => {
        el.setAttribute('max', today);
    });

})();
