from pathlib import Path
p=Path('/opt/struktur/social-media-agent/templates/cockpit.html')
s=p.read_text()
marker='function buildPanel(s) {'
helper=r'''
function communityCommentPlan(s) {
if (!s || !s.mas_relevant || s.content_blocked || !s.url) return null;
const raw = [s.title,s.de,s.sum,s.mas_reason,s.b_winkel].filter(Boolean).join(' ').toLowerCase();
const hasQuestion = /\?|brauche ich|was kann|wie kann|welche|hilfe|problem/.test(raw);
const damp = /schimmel|feucht|kondens|nass|luftfeuchte|humidity/.test(raw);
const vent = /lüft|luftwechsel|ventilation/.test(raw);
const directComp = !!s.comp;
if (!(damp || vent || hasQuestion) || Number(s.fb||0) < 2) return null;
let comment = '';
if (damp && vent) {
comment = 'Eine Lüftungsanlage kann hier sinnvoll sein – aber der Schimmel allein beantwortet die Frage noch nicht. Entscheidend ist zuerst die Ursache: Kondensation an kalten Wandflächen, zu hoher Feuchteeintrag, zu wenig Luftwechsel oder eventuell auch bauliche Feuchte.\n\nWenn erhöhte Raumluftfeuchte und mangelnder Luftwechsel die Ursache sind, kann eine dezentrale Lüftung sehr wirksam sein. Vorher sollte man aber Raumfeuchte, Temperatur und möglichst auch die Oberflächentemperatur der betroffenen Wand messen. Sonst behandelt man unter Umständen nur das Symptom.';
} else if (damp) {
comment = 'Bei sichtbarer Feuchte oder Schimmel würde ich zuerst die Ursache klären, bevor saniert wird. Kondensation, eindringende oder aufsteigende Feuchte und zu geringer Luftwechsel können ähnlich aussehen, brauchen aber unterschiedliche Lösungen. Sinnvoll sind zuerst Raumfeuchte, Temperatur und – wenn möglich – die Oberflächentemperatur der betroffenen Fläche.';
} else {
comment = 'Ob eine Lüftungsanlage sinnvoll ist, hängt vor allem von Nutzung, Raumfeuchte, Luftwechsel und Gebäudesituation ab. Vor einer Entscheidung würde ich deshalb erst messen und die Ursache einordnen. So lässt sich vermeiden, dass eine technische Lösung am eigentlichen Problem vorbeigeht.';
}
return {
useful: directComp ? 'Mit Vorsicht' : 'JA',
risk: directComp ? 'mittel' : 'niedrig',
cta: 'Nein – erster Kommentar rein fachlich',
priority: hasQuestion && (damp || vent) ? 'sehr hoch' : 'hoch',
reason: hasQuestion ? 'Konkrete Frage + MAS-Kernthema' : 'Fachlicher MAS-Bezug mit erkennbarem Beratungswert',
comment
};
}
'''
if 'function communityCommentPlan' not in s:
s=s.replace(marker,helper+'\n'+marker,1)
insert_after=" const relevanceText = (!s.mas_relevant || s.content_blocked) ? 'Kein belastbarer MAS-Bezug für eine Content-Verwertung.' : (s.mas_reason || malText(s.mal, s) || 'Fachlicher Bezug zu Mallorca AirServices erkannt.');\n"
block=r''' const communityPlan = communityCommentPlan(s);
const communityHtml = communityPlan ? `
<div class="ps">
<div class="ps-lbl">Community-Reaktion</div>
<div style="background:rgba(59,130,246,.07);border:1px solid rgba(96,165,250,.22);border-radius:8px;padding:12px;">
<div style="display:grid;grid-template-columns:repeat(4,minmax(0,1fr));gap:8px;margin-bottom:10px;">
<div class="sourceitem"><b>Kommentar sinnvoll</b>${x(communityPlan.useful)}</div>
<div class="sourceitem"><b>Priorität</b>${x(communityPlan.priority)}</div>
<div class="sourceitem"><b>Eigenwerbungsrisiko</b>${x(communityPlan.risk)}</div>
<div class="sourceitem"><b>CTA</b>${x(communityPlan.cta)}</div>
</div>
<div style="font-size:11px;color:#8aa0b5;margin-bottom:7px;">${x(communityPlan.reason)}</div>
<div style="white-space:pre-line;font-size:13px;line-height:1.55;color:#e6edf5;background:#111827;border:1px solid #263447;border-radius:7px;padding:10px;">${x(communityPlan.comment)}</div>
<div style="display:flex;gap:8px;flex-wrap:wrap;margin-top:10px;">
<button type="button" class="pw-btn" onclick="copyCommunityComment('${s.id}')">Kommentar kopieren</button>
<a class="pw-btn" href="${x(s.url)}" target="_blank" rel="noopener" style="text-decoration:none;">Originalpost öffnen ↗</a>
</div>
</div>
</div>` : '';
'''
if 'const communityHtml = communityPlan' not in s:
s=s.replace(insert_after,insert_after+block,1)
needle=' <div class="ps"><div class="ps-lbl">Beitragsideen</div>${ideasHtml}</div>\n'
if '${communityHtml}' not in s:
s=s.replace(needle,needle+' ${communityHtml}\n',1)
copy_fn=r'''
function copyCommunityComment(sid) {
const s = window._S && window._S[sid];
const plan = communityCommentPlan(s);
if (!plan || !plan.comment) { _pwToast('Kein Kommentarvorschlag verfügbar'); return; }
const done = () => _pwToast('Kommentar kopiert');
if (navigator.clipboard && navigator.clipboard.writeText) {
navigator.clipboard.writeText(plan.comment).then(done).catch(()=>fallbackCopy(plan.comment));
} else fallbackCopy(plan.comment);
function fallbackCopy(text) {
const ta=document.createElement('textarea'); ta.value=text; ta.style.position='fixed'; ta.style.opacity='0';
document.body.appendChild(ta); ta.select(); document.execCommand('copy'); ta.remove(); done();
}
}
'''
anchor='// ═══ SMA Publish Workflow ═══════════════════════════════════════════════\n'
if 'function copyCommunityComment' not in s:
s=s.replace(anchor,copy_fn+'\n'+anchor,1)
p.write_text(s)
print('patched', p)