with open("static/js/bottom-bar.js", "r") as f:
    content = f.read()

import re

# Insert a filter state
if "let overviewFilter" not in content:
    content = content.replace("let activeKey = 'timer';", "let activeKey = 'timer';\n    let overviewFilter = null;")

# Update listFor overview so it can show filtered items
old_overview = """        if (key === 'overview') {
            let out = [];
            if (urgent.count > 0) out.push('🚨 Hastesager: ' + urgent.count + ' aktive');
            if (mail.unread > 0) out.push('📧 Ubesvarede mails: ' + mail.unread + ' (' + mail.customer_reply_needed + ' kræver svar)');
            if (cases.open > 0) out.push('📂 Åbne sager i alt: ' + cases.open);
            if (kuma.down > 0) out.push('📉 Uptime Kuma nedetid: ' + kuma.down + ' enheder');
            if (eset.incidents > 0) out.push('🔐 ESET incidents: ' + eset.incidents);
            
            if (out.length === 0) {
                out.push('🎉 Alt ser grønt ud! Intet kritisk lige nu.');
                out.push('👉 Klik på fanerne til venstre for mere info.');
            }
            return out;
        }"""
new_overview = """        if (key === 'overview') {
            if (overviewFilter === 'urgent') return urgent.list ? urgent.list.map(u => '🚨 Hastesag: ' + u.title) : ['Ingen hastesager.'];
            if (overviewFilter === 'kuma') return kuma.list ? kuma.list.map(k => '📉 Nede: ' + k) : ['Alle systemer oppe.'];
            if (overviewFilter === 'eset') return eset.list ? eset.list.map(e => '🔐 Incident: ' + e) : ['Ingen ESET incidents.'];
            if (overviewFilter === 'cases') return cases.list ? cases.list.map(c => '📂 ' + c.title) : ['Ingen åbne sager.'];
            if (overviewFilter === 'mail') return ['📧 ' + mail.unread + ' ulæste mails. ' + mail.customer_reply_needed + ' kundesvar krævet.'];

            let out = [];
            if (urgent.count > 0) out.push('🚨 Hastesager: ' + urgent.count + ' aktive');
            if (mail.unread > 0) out.push('📧 Ubesvarede mails: ' + mail.unread + ' (' + mail.customer_reply_needed + ' kræver svar)');
            if (cases.open > 0) out.push('📂 Åbne sager i alt: ' + cases.open);
            if (kuma.down > 0) out.push('📉 Uptime Kuma nedetid: ' + kuma.down + ' enheder');
            if (eset.incidents > 0) out.push('🔐 ESET incidents: ' + eset.incidents);
            
            if (out.length === 0) {
                out.push('🎉 Alt ser grønt ud! Intet kritisk lige nu.');
                out.push('👉 Klik på fanerne til venstre for mere info.');
            }
            return out;
        }"""
content = content.replace(old_overview, new_overview)

# Update chip click to set filter
old_chip = """                // Map top chip keys to side tabs if applicable, else 'overview'
                let targetTab = 'overview';
                if (key === 'timer') targetTab = 'timer';
                
                const tabBtn = document.querySelector('.bb-tab-btn[data-bb-tab="' + targetTab + '"]');
                if (tabBtn) tabBtn.click();
            });"""
new_chip = """                // Set filter if clicking a generic chip
                let targetTab = 'overview';
                if (key === 'timer') {
                    targetTab = 'timer';
                    overviewFilter = null;
                } else if (['urgent', 'kuma', 'eset', 'cases', 'mail'].indexOf(key) !== -1) {
                    targetTab = 'overview';
                    overviewFilter = key;
                }
                
                const tabBtn = document.querySelector('.bb-tab-btn[data-bb-tab="' + targetTab + '"]');
                if (tabBtn) tabBtn.click();
            });"""
content = content.replace(old_chip, new_chip)

# Clear filter if a generic side tab is clicked
old_bind_tab = """            buttons[i].addEventListener('click', function () {
                for (let j = 0; j < buttons.length; j++) {
                    buttons[j].classList.remove('is-active');
                    buttons[j].setAttribute('aria-selected', 'false');
                }
                this.classList.add('is-active');
                this.setAttribute('aria-selected', 'true');

                activeKey = this.getAttribute('data-bb-tab');
                renderTabPanel();"""
new_bind_tab = """            buttons[i].addEventListener('click', function (e) {
                // Clear filter on direct human click of the button, unless we programmatically called click()
                if (e.isTrusted) overviewFilter = null; 

                for (let j = 0; j < buttons.length; j++) {
                    buttons[j].classList.remove('is-active');
                    buttons[j].setAttribute('aria-selected', 'false');
                }
                this.classList.add('is-active');
                this.setAttribute('aria-selected', 'true');

                activeKey = this.getAttribute('data-bb-tab');
                renderTabPanel();"""
content = content.replace(old_bind_tab, new_bind_tab)

with open("static/js/bottom-bar.js", "w") as f:
    f.write(content)
