#!/usr/bin/env python3
"""Explore Webgains API endpoints to find per-feed product download."""
import requests, json

WEBGAINS_API = "https://platform-api.webgains.com"
PUBLISHER_ID = "1347930"
CAMPAIGN_ID = "1622230"
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}", "Accept": "application/json"}

# Try various endpoint patterns for per-feed product access
endpoints = [
    # Per-feed products
    f"/auth/publishers/{PUBLISHER_ID}/campaigns/{CAMPAIGN_ID}/feeds/18588/products",
    # Feed download
    f"/auth/publishers/{PUBLISHER_ID}/campaigns/{CAMPAIGN_ID}/feeds/18588/download",
    # Program-specific products
    f"/auth/publishers/{PUBLISHER_ID}/campaigns/{CAMPAIGN_ID}/programs/299740/products",
    # Feed export
    f"/auth/publishers/{PUBLISHER_ID}/feeds/18588/products",
    # Direct feed access
    f"/auth/feeds/18588/products",
    # Product search with program filter  
    f"/auth/publishers/{PUBLISHER_ID}/campaigns/{CAMPAIGN_ID}/products",
]

for ep in endpoints:
    url = f"{WEBGAINS_API}{ep}"
    try:
        r = requests.get(url, headers=H, params={"size": 2}, timeout=10)
        status = r.status_code
        body = r.text[:300]
        print(f"{status} {ep}")
        if status == 200:
            print(f"  {body[:200]}")
    except Exception as e:
        print(f"ERR {ep}: {e}")

# Also try: the products endpoint with program_id filter
print("\n=== Try program_id filter ===")
url = f"{WEBGAINS_API}/auth/publishers/{PUBLISHER_ID}/campaigns/{CAMPAIGN_ID}/feeds/products"
for program_id in [299740, 302730]:  # Baskets, Sneakermood
    params = {"programs[]": [program_id], "size": 3, "fields[]": ["title", "brand", "id"]}
    r = requests.get(url, headers=H, params=params, timeout=10)
    data = r.json()
    products = data.get("data", [])
    print(f"\nProgram {program_id}:")
    for p in products:
        print(f"  {p.get('id')} | {p.get('brand')} | {p.get('title','')[:50]}")

# Also try: the correct feeds[] param format
print("\n=== Try different feeds param format ===")
for fmt in [{"feeds": "18588"}, {"feeds": [18588]}, {"feed_id": 18588}, {"feedId": 18588}]:
    params = {**fmt, "size": 2, "fields[]": ["title", "brand", "id"]}
    r = requests.get(url, headers=H, params=params, timeout=10)
    data = r.json()
    products = data.get("data", [])
    total = data.get("recordsTotal", "?")
    if products:
        print(f"  {fmt} -> total={total}, first={products[0].get('brand')} | {products[0].get('title','')[:40]}")
    else:
        print(f"  {fmt} -> {r.status_code} {r.text[:100]}")
