from pathlib import Path
p=Path('/opt/struktur/social-media-agent/app.py')
s=p.read_text()
anchor="function esc(t){return(t||'').replace(/&/g,'&').replace(/</g,'<').replace(/>/g,'>')}\n"
js="""let publishDraftId=null;\nfunction openPublish(id,platform){publishDraftId=id;document.getElementById('pubPlatform').value=(platform||'facebook').toLowerCase();document.getElementById('pubModal').style.display='flex';document.getElementById('pubStatus').textContent='';toggleDue()}\nfunction closePublish(){document.getElementById('pubModal').style.display='none';publishDraftId=null}\nfunction toggleDue(){document.getElementById('dueWrap').style.display=document.getElementById('pubAction').value==='schedule'?'block':'none'}\nasync function sendPublish(){const action=document.getElementById('pubAction').value;const raw=document.getElementById('pubDue').value;const payload={platform:document.getElementById('pubPlatform').value,action,media_kind:document.getElementById('pubMediaKind').value,media_url:document.getElementById('pubMedia').value.trim(),link_url:document.getElementById('pubLink').value.trim(),first_comment:document.getElementById('pubComment').value.trim()};if(action==='schedule'){if(!raw){document.getElementById('pubStatus').textContent=' Termin fehlt';return}payload.due_at=new Date(raw).toISOString()}const r=await fetch('/api/content-draft/'+publishDraftId+'/buffer',{method:'POST',headers:{'Content-Type':'application/json'},body:JSON.stringify(payload)});const d=await r.json();document.getElementById('pubStatus').textContent=d.ok?' Erfolgreich an Buffer übergeben':' Fehler: '+(d.error||r.status);if(d.ok){setTimeout(()=>{closePublish();load()},700)}}\n"""
if 'function openPublish(' not in s:
s=s.replace(anchor,anchor+js,1)
p.write_text(s)
print('MAS_JS_PATCHED')