from pathlib import Path
p=Path('/opt/struktur/asol-runtime/app.py')
s=p.read_text()
start=s.index("function sendBuffer(){{")
end=s.index("</script>", start)
new="""function toggleDue(){{document.getElementById('dueWrap').style.display=document.getElementById('pubAction').value==='schedule'?'block':'none';}}function sendBuffer(){{const action=document.getElementById('pubAction').value;const raw=document.getElementById('pubDue').value;const payload={{signal_id:{item['signal_id']!r},platform:document.getElementById('pubPlatform').value,body:document.getElementById('body').value,action:action,media_url:document.getElementById('pubMedia').value.trim(),media_kind:document.getElementById('pubMediaKind').value,link_url:document.getElementById('pubLink').value.trim(),first_comment:document.getElementById('pubComment').value.trim()}};if(action==='schedule'){{if(!raw){{document.getElementById('saveStatus').textContent='Termin fehlt';return;}}payload.due_at=new Date(raw).toISOString();}}fetch('/api/draft/buffer',{{method:'POST',headers:{{'Content-Type':'application/json'}},body:JSON.stringify(payload)}}).then(r=>r.json()).then(d=>document.getElementById('saveStatus').textContent=d.ok?'Erfolgreich an Buffer übergeben':('Buffer-Fehler: '+(d.error||'unbekannt')));}}"""
s=s[:start]+new+s[end:]
s=s.replace("<select id='pubPlatform'>", "<select id='pubPlatform' data-current='{item['platform']}'>",1)
s=s.replace("</script></body>", "document.getElementById('pubPlatform').value=document.getElementById('pubPlatform').dataset.current==='reels'?'instagram':document.getElementById('pubPlatform').dataset.current;</script></body>",1)
p.write_text(s)
print('ASOL_JS_OK')