/**
 * LayoutManager: Tab-Navigation fuer das Monitor-Dashboard.
 * Nur ein Tab-Panel gleichzeitig sichtbar, pro Lage gemerkt in localStorage.
 */
const LayoutManager = {
    TAB_ORDER: ['zusammenfassung', 'lagebild', 'timeline', 'karte', 'faktencheck', 'pipeline', 'quellen'],
    _currentIncidentId: null,
    _initialized: false,

    init() {
        if (this._initialized) return;
        const nav = document.getElementById('tab-nav');
        if (!nav) return;

        nav.querySelectorAll('.tab-btn').forEach(btn => {
            btn.addEventListener('click', () => {
                const tab = btn.getAttribute('data-tab');
                if (tab) this.switchTab(tab);
            });
        });

        nav.style.display = '';
        this._initialized = true;
    },

    switchTab(tabId, save = true) {
        if (!this.TAB_ORDER.includes(tabId)) tabId = 'zusammenfassung';

        document.querySelectorAll('#tab-nav .tab-btn').forEach(b => {
            b.classList.toggle('active', b.getAttribute('data-tab') === tabId);
        });
        document.querySelectorAll('.tab-panel').forEach(p => {
            p.classList.toggle('active', p.id === 'panel-' + tabId);
        });

        // Leaflet-Karte: invalidateSize nach Panel-Wechsel, damit Tiles korrekt rendern
        if (tabId === 'karte' && typeof UI !== 'undefined' && UI._map) {
            setTimeout(() => { try { UI._map.invalidateSize(); } catch (e) { /* ignore */ } }, 50);
        }

        if (save && this._currentIncidentId != null) {
            try {
                localStorage.setItem('osint_tab_' + this._currentIncidentId, tabId);
            } catch (e) { /* quota */ }
        }
    },

    restoreTabFor(incidentId) {
        this._currentIncidentId = incidentId;
        let target = 'zusammenfassung';
        try {
            const saved = localStorage.getItem('osint_tab_' + incidentId);
            if (saved && this.TAB_ORDER.includes(saved)) target = saved;
        } catch (e) { /* ignore */ }
        this.switchTab(target, false);
    },

    /** Tab-Labels je Incident-Typ anpassen (adhoc vs. research). */
    applyTypeLabels(incidentType) {
        const isResearch = incidentType === 'research';
        const zf = document.querySelector('#tab-nav .tab-btn[data-tab="zusammenfassung"]');
        const lb = document.querySelector('#tab-nav .tab-btn[data-tab="lagebild"]');
        const _t = (k, fb) => (typeof T === 'function') ? T(k, fb) : fb;
        if (zf) zf.textContent = isResearch
            ? _t('tab.summary_short', 'Zusammenfassung')
            : _t('tab.latest_developments', 'Neueste Entwicklungen');
        if (lb) lb.textContent = isResearch
            ? _t('tab.summary_report', 'Recherchebericht')
            : _t('tab.summary', 'Lagebild');
    },

    // Legacy-API-Stubs: falls alte Aufrufe im Code liegen, stumm schlucken statt crashen.
    toggleTile() { /* legacy no-op */ },
    reset() { /* legacy no-op */ },
    save() { /* legacy no-op */ },
    resizeTileToContent() { /* legacy no-op */ },
    destroy() { /* legacy no-op */ },
};

document.addEventListener('DOMContentLoaded', () => LayoutManager.init());
