import re

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

events_html = """
            if (btn.id === 'btnNextTask') {
                console.log("-> Beder backend om næste opgave...");
                btn.innerHTML = '<span class="spinner-border spinner-border-sm" role="status" aria-hidden="true"></span> Omsætter kalender og SLA...';
                btn.disabled = true;
                
                fetch('/api/v1/bottom-bar/next_task', { method: 'POST' })
                .then(r => r.json())
                .then(data => {
                    const task = data.task;
                    btn.innerHTML = '<i class="bi bi-magic me-2"></i>Du fik tildelt: ' + task.title + ' (Sag #' + task.case_id + ') <span class="badge bg-light text-dark ms-2">' + data.free_time_calculated + 'm fri</span>';
                    btn.classList.add('btn-success');
                    btn.classList.remove('btn-primary');
                })
                .catch(err => {
                    console.error("Fejl:", err);
                    btn.innerHTML = "Fejl - prøv igen";
                    btn.disabled = false;
                });
            }

            if (btn.id === 'btnSendMsg') {
                const input = document.getElementById('chatInputQuick');
                const recipientObj = document.getElementById('chatRecipient');
                
                if (input && input.value.trim() !== '') {
                    const recipient = recipientObj ? recipientObj.options[recipientObj.selectedIndex].text : 'Alle';
                    
                    console.log("-> Sender besked til", recipient, ":", input.value);
                    
                    const msgVal = input.value;
                    input.value = '';
                    
                    const msgContainer = document.createElement('div');
                    msgContainer.className = 'mb-2 text-end';
                    msgContainer.innerHTML = '<div class="small text-muted mb-1 me-1" style="font-size:0.7rem;">Til: ' + recipient + '</div><div class="d-inline-block bg-primary text-white p-2 rounded-3 text-start shadow-sm" style="max-width: 85%;"><strong>Mig:</strong> ' + msgVal + '</div>';
                    
                    const listUl = listContainer.querySelector('ul') || listContainer;
                    listUl.appendChild(msgContainer);
                    
                    // Simple hacky scroll down
                    const tabInner = document.getElementById('bbTabInnerContent');
                    if(tabInner) {
                        tabInner.scrollTop = tabInner.scrollHeight + 500;
                    }
                }
            }
"""

pattern = r"            if \(btn\.id === 'btnNextTask'\) \{.*?\n            \}"
text = re.sub(pattern, events_html.strip(), text, flags=re.DOTALL)

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