with open("js/shopify_js_functions.js", "r") as f:
    shopify_js = f.read()

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

# Insert Shopify functions right after saveWPCreds closing brace + checkWPStatus block
anchor = "async function checkWPStatus() {"
idx = content.find(anchor)
if idx == -1:
    print("ERROR: checkWPStatus anchor not found")
else:
    # Find end of checkWPStatus function
    end = content.find("\nasync function ", idx + 1)
    if end == -1:
        end = content.find("\nfunction ", idx + 1)
    content = content[:end] + "\n" + shopify_js + content[end:]
    with open("public_html/supercerebro/js/app.js", "w") as f:
        f.write(content)
    print("OK app.js: Shopify functions inserted, new length:", len(content))
