#!/usr/bin/env python3
"""Check pagination response and try to get >1000 products."""
import requests, json

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"
PUBLISHER_ID = "1347930"
H = {"Authorization": f"Bearer {TOKEN}", "Accept": "application/json"}
url = f"https://platform-api.webgains.com/auth/publishers/{PUBLISHER_ID}/campaigns/1622230/feeds/products"

# Check pagination response
params = {"feedIds[]": [18588], "size": 5}
r = requests.get(url, headers=H, params=params, timeout=10)
data = r.json()
print("Pagination field:", json.dumps(data.get("pagination"), indent=2))

# Check: how many unique products can we get from 1000?
print("\n=== Get 1000 from each feed ===")
for fid, name in [(18588, "Baskets"), (16158, "Sneakermood"), (25578, "Patta")]:
    params = {"feedIds[]": [fid], "size": 1000}
    r = requests.get(url, headers=H, params=params, timeout=60)
    data = r.json()
    prods = data.get("data", [])
    unique_ids = len(set(str(p.get("id")) for p in prods))
    brands = {}
    categories = {}
    for p in prods:
        b = p.get("brand", "?")
        brands[b] = brands.get(b, 0) + 1
        cat = p.get("product_type")
        if isinstance(cat, list):
            cat = cat[0] if cat else "?"
        categories[str(cat)] = categories.get(str(cat), 0) + 1
    
    print(f"\n{name} (feed {fid}): {len(prods)} products, {unique_ids} unique")
    print(f"  Top brands: {sorted(brands.items(), key=lambda x: -x[1])[:5]}")
    print(f"  Categories: {sorted(categories.items(), key=lambda x: -x[1])[:10]}")
    
    # Pagination info
    pag = data.get("pagination", {})
    print(f"  Pagination: {pag}")

# Check if we can use search/filter within feedIds to paginate
print("\n=== Try search within feed ===")
params = {"feedIds[]": [18588], "size": 1000, "search": "a"}
r = requests.get(url, headers=H, params=params, timeout=30)
data = r.json()
print(f"Search 'a': {len(data.get('data',[]))} results")

params = {"feedIds[]": [18588], "size": 1000, "search": "nike"}
r = requests.get(url, headers=H, params=params, timeout=30)
data = r.json()
print(f"Search 'nike': {len(data.get('data',[]))} results")

params = {"feedIds[]": [18588], "size": 1000, "search": "adidas"}
r = requests.get(url, headers=H, params=params, timeout=30)
data = r.json()
print(f"Search 'adidas': {len(data.get('data',[]))} results")
