with open("public_html/supercerebro/sections/seo.php", "r") as f:
    content = f.read()

# 1. Add Shopify connect card after WP connect card closing div
shopify_connect_card = """
    <!-- Conexión Shopify -->
    <div class="card" style="margin-bottom:14px" id="shopify-connect-card">
      <div class="chd">
        <div>
          <div class="ctit">🛍 Shopify — Publicación directa</div>
          <div style="font-size:11px;color:var(--m);margin-top:2px">Conecta Shopify para publicar artículos del blog con un clic</div>
        </div>
        <div id="shopify-status-badge" style="font-size:11px;padding:3px 10px;border-radius:10px;background:rgba(250,204,21,.1);border:1px solid rgba(250,204,21,.25);color:var(--y)">Sin configurar</div>
      </div>
      <div id="shopify-connect-form" style="display:grid;grid-template-columns:1fr 1fr;gap:10px;margin-top:10px">
        <div><div style="font-size:11px;color:var(--m);margin-bottom:4px">Dominio de la tienda</div><input type="text" id="shopify-domain-seo" placeholder="mitienda.myshopify.com" class="ctx-ta" style="height:34px;padding:6px 10px;font-size:12px"></div>
        <div><div style="font-size:11px;color:var(--m);margin-bottom:4px">Admin API Access Token</div><input type="password" id="shopify-token-seo" placeholder="shpat_..." class="ctx-ta" style="height:34px;padding:6px 10px;font-size:12px"></div>
      </div>
      <div style="display:flex;gap:8px;align-items:center;margin-top:10px;flex-wrap:wrap">
        <button onclick="saveShopifyCredsSEO()" style="padding:6px 16px;border-radius:7px;background:#96bf4d;border:none;color:#fff;cursor:pointer;font-family:var(--fb);font-size:12px;font-weight:600">Guardar conexión</button>
        <button id="shopify-test-btn" onclick="testShopifyConnection()" style="padding:6px 14px;border-radius:7px;background:var(--bg3);border:1px solid var(--br2);color:var(--t);cursor:pointer;font-family:var(--fb);font-size:12px">Probar conexión</button>
        <span id="shopify-save-status-seo" style="font-size:12px;color:var(--m)"></span>
      </div>
    </div>
"""

# Insert after the closing </div> of WP connect card
wp_card_end = '    </div>\n\n    <!-- Generador de artículos SEO -->'
shopify_inject = '    </div>\n' + shopify_connect_card + '\n    <!-- Generador de artículos SEO -->'

if wp_card_end in content:
    content = content.replace(wp_card_end, shopify_inject, 1)
    print("OK seo.php: Shopify connect card added")
else:
    # try alternate
    wp_card_end2 = '    </div>\n    <!-- Generador de artículos SEO -->'
    if wp_card_end2 in content:
        content = content.replace(wp_card_end2, shopify_inject.replace('\n\n    <!-- Generador', '\n    <!-- Generador'), 1)
        print("OK seo.php (alt): Shopify connect card added")
    else:
        print("ERROR: WP card end anchor not found")
        idx = content.find('<!-- Generador de artículos SEO -->')
        print("Generador idx:", idx)
        print(repr(content[idx-200:idx]))

# 2. Add Shopify publish button next to WP publish button
old_btn = '            <button id="wp-publish-btn" onclick="publishToWordPress()" style="background:rgba(52,152,219,.12);border:1px solid rgba(52,152,219,.35);border-radius:6px;padding:4px 10px;font-size:11px;color:#3498db;cursor:pointer;font-family:var(--fb);font-weight:600">📤 Publicar en WordPress</button>'
new_btn = old_btn + """
            <button onclick="toggleShopifyPublishOpts()" id="shopify-opts-toggle" style="background:rgba(150,191,77,.08);border:1px solid rgba(150,191,77,.25);border-radius:6px;padding:4px 10px;font-size:11px;color:#96bf4d;cursor:pointer;font-family:var(--fb)">⚙ Opciones Shopify</button>
            <button id="shopify-publish-btn" onclick="publishToShopify()" style="background:rgba(150,191,77,.12);border:1px solid rgba(150,191,77,.35);border-radius:6px;padding:4px 10px;font-size:11px;color:#96bf4d;cursor:pointer;font-family:var(--fb);font-weight:600">🛍 Publicar en Shopify</button>"""

if old_btn in content:
    content = content.replace(old_btn, new_btn, 1)
    print("OK seo.php: Shopify publish button added")
else:
    print("ERROR: WP publish button anchor not found")

# 3. Add Shopify publish options panel after WP publish opts panel
old_panel_end = '        <div id="seo-article-rendered" class="avi-resp" style="display:block;max-height:600px;overflow-y:auto"></div>'
shopify_panel = """        <!-- Panel opciones de publicación Shopify (oculto por defecto) -->
        <div id="shopify-publish-opts" style="display:none;background:rgba(150,191,77,.06);border:1px solid rgba(150,191,77,.2);border-radius:8px;padding:12px;margin-bottom:10px">
          <div style="font-size:11px;font-weight:700;color:#96bf4d;text-transform:uppercase;letter-spacing:.5px;margin-bottom:10px">Opciones de publicación Shopify</div>
          <div style="display:grid;grid-template-columns:1fr 1fr;gap:8px">
            <div>
              <label style="font-size:11px;color:var(--m);display:block;margin-bottom:3px">Blog de destino</label>
              <select id="shopify-blog-sel" style="width:100%;box-sizing:border-box;background:var(--bg3);border:1px solid var(--br2);border-radius:6px;padding:5px 8px;font-size:12px;color:var(--t);font-family:var(--fb)">
                <option value="">Cargando blogs...</option>
              </select>
            </div>
            <div>
              <label style="font-size:11px;color:var(--m);display:block;margin-bottom:3px">Handle (slug URL) <span style="font-weight:400;opacity:.7">(opcional)</span></label>
              <input id="shopify-handle" type="text" placeholder="mi-articulo-seo" style="width:100%;box-sizing:border-box;background:var(--bg3);border:1px solid var(--br2);border-radius:6px;padding:5px 8px;font-size:12px;color:var(--t);font-family:var(--fb)">
            </div>
            <div>
              <label style="font-size:11px;color:var(--m);display:block;margin-bottom:3px">Tags <span style="font-weight:400;opacity:.7">(separados por coma)</span></label>
              <input id="shopify-tags" type="text" placeholder="seo, marketing, tips" style="width:100%;box-sizing:border-box;background:var(--bg3);border:1px solid var(--br2);border-radius:6px;padding:5px 8px;font-size:12px;color:var(--t);font-family:var(--fb)">
            </div>
            <div>
              <label style="font-size:11px;color:var(--m);display:block;margin-bottom:3px">Estado</label>
              <div style="display:flex;gap:6px;margin-top:4px">
                <label style="display:flex;align-items:center;gap:5px;cursor:pointer;font-size:12px;color:var(--t)">
                  <input type="radio" name="shopify-status" value="draft" checked style="accent-color:#96bf4d"> Oculto
                </label>
                <label style="display:flex;align-items:center;gap:5px;cursor:pointer;font-size:12px;color:var(--t)">
                  <input type="radio" name="shopify-status" value="publish" style="accent-color:#96bf4d"> Publicar ahora
                </label>
              </div>
            </div>
          </div>
        </div>
        <div id="seo-article-rendered" class="avi-resp" style="display:block;max-height:600px;overflow-y:auto"></div>"""

if old_panel_end in content:
    content = content.replace(old_panel_end, shopify_panel, 1)
    print("OK seo.php: Shopify options panel added")
else:
    print("ERROR: seo-article-rendered anchor not found")

with open("public_html/supercerebro/sections/seo.php", "w") as f:
    f.write(content)
print("seo.php saved")
