#!/usr/bin/env python3
"""Check Webgains API for feed download/export endpoints."""
import requests
import json

WEBGAINS_API = "https://platform-api.webgains.com"
PUBLISHER_ID = "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"

HEADERS = {
    "Authorization": f"Bearer {TOKEN}",
    "Accept": "application/json",
}

CAMP_ID = "1622230"
FEED_ID = "18588"  # Baskets

# Try various endpoints for feed download
endpoints = [
    f"/auth/publishers/{PUBLISHER_ID}/campaigns/{CAMP_ID}/feeds/{FEED_ID}",
    f"/auth/publishers/{PUBLISHER_ID}/campaigns/{CAMP_ID}/feeds/{FEED_ID}/download",
    f"/auth/publishers/{PUBLISHER_ID}/campaigns/{CAMP_ID}/feeds/{FEED_ID}/export",
    f"/auth/publishers/{PUBLISHER_ID}/campaigns/{CAMP_ID}/feeds/{FEED_ID}/products",
]

for ep in endpoints:
    url = f"{WEBGAINS_API}{ep}"
    print(f"\n--- GET {ep} ---")
    try:
        r = requests.get(url, headers=HEADERS, params={"size": 3}, timeout=15)
        print(f"Status: {r.status_code}")
        if r.status_code == 200:
            data = r.json()
            if isinstance(data, dict) and 'data' in data:
                items = data['data']
                if isinstance(items, list) and items:
                    print(f"Items: {len(items)}")
                    # Show first item keys and sample
                    first = items[0]
                    print(f"Keys: {list(first.keys())}")
                    # Check for google_product_category
                    for k in ['google_product_category', 'product_type', 'category', 'categories']:
                        if k in first:
                            print(f"  {k}: {first[k]}")
                    print(json.dumps(first, indent=2, default=str)[:500])
                else:
                    print(json.dumps(data, indent=2, default=str)[:500])
            else:
                print(json.dumps(data, indent=2, default=str)[:500])
        else:
            print(r.text[:200])
    except Exception as e:
        print(f"Error: {e}")
