import re

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

replacement = """
            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 + ')';
                    btn.classList.add('btn-success');
                    btn.classList.remove('btn-primary');
                    
                    // Notificer bruger ift kalender
                    console.log("Routing besked:", data.message);
                    alert("Opgave allokeret. " + data.message);
                })
                .catch(err => {
                    console.error("Fejl:", err);
                    btn.innerHTML = "Fejl - prøv igen";
                    btn.disabled = false;
                });
            }
"""

text = re.sub(r'if \(btn\.id === \'btnNextTask\'\) \{.*?btn\.classList\.remove\(\'btn-primary\'\);\n                \}, 1000\);\n            \}', replacement.strip(), text, flags=re.DOTALL)

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