#!/usr/bin/env python3
"""Check if Webgains feeds have download URLs"""
import requests, json

API = "https://platform-api.webgains.com"
PUB = "1347930"
TOKEN = "eyJ0eXAiOiJKV1QiLCJhbGciOiJSUzI1NiJ9.eyJhdWQiOiIxMDAwMzEiLCJqdGkiOiIwNjM5ODEwYzQ0YzM1MTBlZmZiMGRkYWNiM2EyNWFlNTg2YzFmNmQ5ODNjNGQ2MDA4NzkwZmM0NTkzY2EyNjg4ZGViZDE4NTgwOThmYjJjYiIsImlhdCI6MTc3MTUxNjc4OS44MzAxMzUsIm5iZiI6MTc3MTUxNjc4OS44MzAxMzcsImV4cCI6MjA4NzA0OTU4OS44MjcwODgsInN1YiI6IjIwMTcwOCIsInNjb3BlcyI6W119.ZxlCDsyBI7_SNUWjO-PUxDYKvQVfuUBzU5n-SgqHdAZCnOj9253yVcJIS5NRXJLrnyx0E_9bn8ymfXS71cvPISA4hPAlKCSMjdkv9f2k2FFwdAlQQ6mdhBB2m9dzHUIn34QwA8s4-ikB1ATKIZeqiJL-3Gh9eiMYPpvaJdCCjN-oGPMhhlehkgA04cZSgwy6HfKgb6uPvUSG9w_tvN8U06uEHKV11DedQHNWWGzuXpd2FEL3baV0z9KjSNMZsJZHx2yHUb6gVWJwaR7_louahMvrPXG-YLgnGvNMcPDPjm-z5HJ-JKcPv9_FBqAYGItZxgeGgVoS8MVhW6lc1a-6CvYtVanfXF42MPPNP-Qq76qRtHPLRBdkIATMuBxVJrMzlprT3n4F0sUUltGR4ixZuzMUm5LeyPMHgmjy2UfYzsl9eVqx0oJCmKUyY9maBXwFLJa8J1NHM_nWp3hPpCGkMyhzHK1DIf_z8Wirg1hp5vKts93eERWJBlQwhucUUS_KnZeC_gVaQ7kT4jCwaEWkpPxPxyXWRoK0mcYS_JGz3JbpVhRLj5DVYIB9k54DXqjYlCx8ezJZE9sf2SX3VWvPvD1xSQD1caTCm0To0B-MkeU1YpEfWdUslpCJXrpOy2Wbs8iDHR1BzNrMnOEr4zbz_EoB4JJe3tGTGjzNLNIw6XI"
H = {"Authorization": f"Bearer {TOKEN}"}

# Get feeds from manify campaign
r = requests.get(f"{API}/auth/publishers/{PUB}/campaigns/1622230/feeds", headers=H, params={"size": 100})
feeds = r.json().get("data", [])

# Print ALL fields of first feed to find download URL
print("=== First feed - ALL fields ===")
print(json.dumps(feeds[0], indent=2, default=str))

print("\n=== All feeds summary ===")
for f in feeds:
    fid = f["id"]
    name = f.get("name", "")
    count = f.get("products_successfully_stored", 0)
    # Look for any URL fields
    urls = {k: v for k, v in f.items() if isinstance(v, str) and "http" in v}
    print(f"  {fid} | {name} | {count} products | urls: {urls}")

# Also try getting a single feed's detail
print("\n=== Single feed detail ===")
fid = feeds[0]["id"]
for endpoint in [
    f"{API}/auth/publishers/{PUB}/feeds/{fid}",
    f"{API}/auth/publishers/{PUB}/campaigns/1622230/feeds/{fid}",
    f"{API}/auth/publishers/{PUB}/campaigns/1622230/feeds/{fid}/download",
]:
    try:
        r = requests.get(endpoint, headers=H, timeout=10)
        print(f"\n{endpoint} -> {r.status_code}")
        if r.status_code == 200:
            ct = r.headers.get("content-type", "")
            if "json" in ct:
                print(json.dumps(r.json(), indent=2, default=str)[:1000])
            else:
                print(f"Content-Type: {ct}, size: {len(r.content)}")
                print(r.text[:500])
    except Exception as e:
        print(f"{endpoint} -> ERROR: {e}")
