#!/usr/bin/env python3
"""Sample ladify campaign feeds and check Baskets feed origin."""
import requests

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"}

SHOE_WORDS = ['sneaker', 'shoe', 'trainer', 'runner', 'boot', 'slide', 'sandal',
              'dunk', 'force 1', 'air max', 'jordan', 'yeezy', 'ultraboost',
              'superstar', 'old skool', 'gel-', '990', '550', 'speedcat', 'spezial', 'schoen']

# Check ladify feeds
feeds_to_check = [
    ("1622250", 3891, "Polar NL datafeed"),
    ("1622250", 20068, "Standaard feed"),
    ("1622250", 20208, "Exclusivexitems"),
    ("1622250", 18808, "Dickies Life NL"),
]

for camp_id, feed_id, feed_name in feeds_to_check:
    print(f"\n{'='*60}")
    print(f"Feed: {feed_name} ({feed_id}) [campaign {camp_id}]")
    url = f"{WEBGAINS_API}/auth/publishers/{PUBLISHER_ID}/campaigns/{camp_id}/feeds/products"
    params = {"feedIds[]": [feed_id], "size": 50}
    try:
        r = requests.get(url, headers=HEADERS, params=params, timeout=30)
        data = r.json()
        products = data.get("data", [])
        total = data.get("pagination", {}).get("total", "?")
        print(f"Products: {len(products)}, Total: {total}")
        
        cats = {}
        sneaker_count = 0
        for p in products:
            cat = p.get('product_type') or ['Unknown']
            if isinstance(cat, list):
                cat = cat[0] if cat else 'Unknown'
            cat = str(cat).strip()[:40]
            cats[cat] = cats.get(cat, 0) + 1
            title = (p.get('title') or '').lower()
            if any(w in title for w in SHOE_WORDS):
                sneaker_count += 1
        
        print(f"Categories: {sorted(cats.items(), key=lambda x: -x[1])[:8]}")
        print(f"Sneaker matches: {sneaker_count}/{len(products)}")
        for p in products[:5]:
            print(f"  {p.get('brand','?'):15s} | {str(p.get('product_type','?'))[:25]:25s} | {p.get('title','?')[:50]}")
    except Exception as e:
        print(f"Error: {e}")

# Also check where Baskets feed (18588) is - try all campaigns
print(f"\n{'='*60}")
print("Looking for Baskets feed (18588) across campaigns...")
for camp_id in ["1622230", "1622250", "1622240", "1622260"]:
    url = f"{WEBGAINS_API}/auth/publishers/{PUBLISHER_ID}/campaigns/{camp_id}/feeds"
    r = requests.get(url, headers=HEADERS, timeout=15)
    feeds = r.json() if r.status_code == 200 else []
    if isinstance(feeds, dict):
        feeds = feeds.get('data', feeds.get('feeds', []))
    for f in feeds:
        if str(f.get('id')) == '18588':
            print(f"  Found Baskets in campaign {camp_id}: {f}")
