#!/usr/bin/env python3
"""Test if Webgains feeds return different data per feed ID."""
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}"}

# 1. Check feed metadata
print("=== Feed Metadata ===")
url = f"{WEBGAINS_API}/auth/publishers/{PUBLISHER_ID}/campaigns/{CAMPAIGN_ID}/feeds"
r = requests.get(url, headers=H, params={"size": 200}, timeout=30)
feeds = r.json().get("data", [])
target_ids = {18588, 25578, 23108, 36168, 35896, 35808, 35630, 16158, 26388, 34710}
for f in feeds:
    if f["id"] in target_ids:
        prog = f.get("program", {})
        print(f"  Feed {f['id']}: {f.get('name')} | program={prog.get('name')} | products={f.get('products_successfully_stored')}")

# 2. Compare product data between feeds
print("\n=== Product Comparison ===")
test_feeds = [18588, 16158, 25578]
first_ids = {}
for fid in test_feeds:
    url2 = f"{WEBGAINS_API}/auth/publishers/{PUBLISHER_ID}/campaigns/{CAMPAIGN_ID}/feeds/products"
    r2 = requests.get(url2, headers=H, params={"feeds[]": [fid], "size": 5, "fields[]": ["title", "brand", "id"]}, timeout=30)
    data = r2.json()
    products = data.get("data", [])
    total = data.get("recordsTotal", "?")
    ids = [p.get("id") for p in products]
    first_ids[fid] = ids
    print(f"\nFeed {fid} (total={total}):")
    for p in products:
        print(f"  {p.get('id')} | {p.get('brand')} | {p.get('title','')[:60]}")

# 3. Check if feeds return identical data
print("\n=== Duplicate Check ===")
if first_ids.get(18588) == first_ids.get(16158):
    print("⚠️  FEEDS 18588 AND 16158 RETURN IDENTICAL DATA — API IS BROKEN!")
else:
    print("✅ Feeds return different data")

# 4. Try the feed download URL approach
print("\n=== Feed Download URLs ===")
for f in feeds:
    if f["id"] in {18588, 16158, 25578}:
        download_url = f.get("download_url") or f.get("url") or f.get("feed_url")
        print(f"  Feed {f['id']}: download_url={download_url}")
        # Print all keys
        print(f"    Keys: {list(f.keys())}")
        if f["id"] == 18588:
            print(f"    Full data: {json.dumps(f, indent=2, default=str)[:1000]}")
