with open("public_html/supercerebro/js/app.js", "r") as f:
    lines = f.readlines()

new_block = [
    "function showPage_config() {\n",
    "  if (_origShowPageConfig) _origShowPageConfig();\n",
    "  loadWhatsAppConfig();\n",
    "  loadGlobalConfigHints();\n",
    "  if (currentClient && currentClient !== 'all') loadAPIsCredentials(currentClient);\n",
    "}\n",
    "\n",
    "async function loadGlobalConfigHints() {\n",
    "  try {\n",
    "    const g = await fetch('api/get-config.php').then(x => x.json());\n",
    "    if (!g.ok) return;\n",
    "    const set = (id, val) => { const el = document.getElementById(id); if (el && val) el.placeholder = val; };\n",
    "    set('claude-key',  g.claude_key_hint);\n",
    "    set('meta-tok',    g.meta_token_hint);\n",
    "    set('meta-app',    g.meta_app_id);\n",
    "    set('g-tok',       g.google_dev_token_hint);\n",
    "    set('g-oauth-cid', g.google_client_id);\n",
    "    set('g-mcc-id',    g.google_mcc_id);\n",
    "    if (g.claude_key_hint)       { const el=document.getElementById('s-claude'); if(el&&!el.textContent.trim()){el.style.color='var(--g)';el.textContent='\\u2713 API Key configurada';} }\n",
    "    if (g.meta_token_hint)       { const el=document.getElementById('s-meta');   if(el&&!el.textContent.trim()){el.style.color='var(--g)';el.textContent='\\u2713 Token configurado';} }\n",
    "    if (g.google_dev_token_hint) { const el=document.getElementById('s-google'); if(el&&!el.textContent.trim()){el.style.color='var(--g)';el.textContent='\\u2713 Credenciales configuradas';} }\n",
    "  } catch(e) {}\n",
    "}\n",
]

patched = False
for i, line in enumerate(lines):
    if "function showPage_config() {" in line:
        lines[i:i+5] = new_block
        patched = True
        print("OK patched at line", i+1)
        break

if patched:
    with open("public_html/supercerebro/js/app.js", "w") as f:
        f.writelines(lines)
    print("Saved")
else:
    print("ERROR: not found")
