#!/usr/bin/env python3
"""Try all possible cursor/pagination params"""
import requests, json

API = "https://platform-api.webgains.com"
PUB = "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"
H = {"Authorization": f"Bearer {TOKEN}"}
CAMP = "1622230"

url = f"{API}/auth/publishers/{PUB}/campaigns/{CAMP}/feeds/products"
cursor = "302970_25538_55209389392248"

# The cursor format is: something_feedid_productid
# Try various param names for cursor pagination
test_params = [
    ("search_cursor", cursor),
    ("cursor", cursor),
    ("after", cursor),
    ("search_after[]", cursor.split("_")),  # Maybe it's an array?
    ("sort_after", cursor),
    ("pit", cursor),
]

# Get baseline IDs first
r = requests.get(url, headers=H, params={
    "feeds[]": [17108],
    "size": 100,
    "fields[]": ["id"],
}, timeout=30)
base_ids = set(p["id"] for p in r.json().get("data", []))
print(f"Baseline: {len(base_ids)} products")

for param_name, param_val in test_params:
    params = {
        "feeds[]": [17108],
        "size": 100,
        "fields[]": ["id"],
        param_name: param_val,
    }
    r = requests.get(url, headers=H, params=params, timeout=30)
    data = r.json()
    if "data" in data:
        ids = set(p["id"] for p in data["data"])
        overlap = len(ids & base_ids)
        print(f"  {param_name}={str(param_val)[:40]}: {len(ids)} products, overlap={overlap}")
    else:
        print(f"  {param_name}: {r.status_code} - {r.text[:200]}")

# Also check if there's a different products endpoint per-feed
print("\n=== Try different URL patterns ===")
alt_urls = [
    f"{API}/auth/publishers/{PUB}/feeds/17108/products",
    f"{API}/auth/publishers/{PUB}/products",
    f"{API}/auth/publishers/{PUB}/campaigns/{CAMP}/products",
    f"{API}/auth/feeds/17108/products",
]
for aurl in alt_urls:
    try:
        r = requests.get(aurl, headers=H, params={"size": 5, "fields[]": ["id", "title"]}, timeout=10)
        print(f"  {aurl.split('publishers/')[-1]}: {r.status_code}")
        if r.status_code == 200:
            data = r.json()
            print(f"    Got: {len(data.get('data', []))} products")
    except Exception as e:
        print(f"  {aurl.split('publishers/')[-1]}: ERROR")
