// /home/Autopartes/pos/static/js/reports.js
// Reports module: sales reports, inventory reports, financial reports

const Reports = (() => {
    function token() {
        return localStorage.getItem('pos_token') || '';
    }

    function checkAuth() {
        if (!token()) {
            window.location.href = '/pos/login';
            return false;
        }
        return true;
    }

    function headers() {
        return { 'Authorization': `Bearer ${token()}`, 'Content-Type': 'application/json' };
    }

    function fmt(n) {
        return parseFloat(n || 0).toLocaleString('es-MX', { minimumFractionDigits: 2, maximumFractionDigits: 2 });
    }

    // -------------------------------------------------------------------------
    // Theme switcher
    // -------------------------------------------------------------------------
    function setTheme(theme) {
        document.documentElement.setAttribute('data-theme', theme);
        try { localStorage.setItem('nexus-theme', theme); } catch(e) {}
        var btnInd = document.getElementById('btn-industrial');
        var btnMod = document.getElementById('btn-modern');
        if (btnInd) btnInd.classList.toggle('is-active', theme === 'industrial');
        if (btnMod) btnMod.classList.toggle('is-active', theme === 'modern');
    }
    window.setTheme = setTheme;

    // -------------------------------------------------------------------------
    // Tab switcher
    // -------------------------------------------------------------------------
    function switchTab(id, btn) {
        document.querySelectorAll('.tab-panel').forEach(function(p) { p.classList.remove('is-active'); });
        document.querySelectorAll('.tab-btn').forEach(function(b) { b.classList.remove('is-active'); });
        var panel = document.getElementById('panel-' + id);
        if (panel) panel.classList.add('is-active');
        if (btn) btn.classList.add('is-active');
    }
    window.switchTab = switchTab;

    // -------------------------------------------------------------------------
    // Live clock
    // -------------------------------------------------------------------------
    function updateClock() {
        var el = document.getElementById('live-clock');
        if (!el) return;
        var now = new Date();
        var pad = function(n) { return String(n).padStart(2, '0'); };
        el.textContent = pad(now.getHours()) + ':' + pad(now.getMinutes()) + ':' + pad(now.getSeconds());
    }

    // -------------------------------------------------------------------------
    // Placeholder API calls
    // -------------------------------------------------------------------------
    async function loadSalesReport(params) {
        // TODO: call /pos/api/cashregister/... with date range
    }

    async function loadInventoryReport() {
        // TODO: call /pos/api/inventory/products for stock report
    }

    async function loadFinancialReport(params) {
        // TODO: call /pos/api/accounting/... for financial reports
    }

    // -------------------------------------------------------------------------
    // Init
    // -------------------------------------------------------------------------
    function init() {
        if (!checkAuth()) return;

        // Restore theme
        try {
            var saved = localStorage.getItem('nexus-theme') || 'industrial';
            setTheme(saved);
        } catch(e) {}

        // Start clock
        updateClock();
        setInterval(updateClock, 1000);
    }

    document.addEventListener('DOMContentLoaded', init);

    return {
        init, setTheme, switchTab,
        loadSalesReport, loadInventoryReport, loadFinancialReport, fmt
    };
})();
