#!/usr/bin/env python3
"""Test Webgains pagination properly"""
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"
FEED = "17108"  # Van Arendonk NL new - 21850 products

url = f"{API}/auth/publishers/{PUB}/campaigns/{CAMP}/feeds/products"
base_params = {
    "feeds[]": [FEED],
    "size": 1000,
    "fields[]": ["title", "id", "brand"],
}

# Fetch page 0
r = requests.get(url, headers=H, params={**base_params, "offset": 0}, timeout=60)
data = r.json()
page0_ids = [p["id"] for p in data.get("data", [])]
total = data.get("recordsTotal", 0)
print(f"Page 0: {len(page0_ids)} products, recordsTotal={total}")
print(f"  First ID: {page0_ids[0] if page0_ids else 'N/A'}")
print(f"  Last ID: {page0_ids[-1] if page0_ids else 'N/A'}")

# Fetch page 1
r = requests.get(url, headers=H, params={**base_params, "offset": 1000}, timeout=60)
data = r.json()
page1_ids = [p["id"] for p in data.get("data", [])]
print(f"\nPage 1 (offset=1000): {len(page1_ids)} products")
print(f"  First ID: {page1_ids[0] if page1_ids else 'N/A'}")
print(f"  Last ID: {page1_ids[-1] if page1_ids else 'N/A'}")

# Check overlap
overlap = set(page0_ids) & set(page1_ids)
print(f"\nOverlap: {len(overlap)} / {len(page0_ids)} ({100*len(overlap)//max(len(page0_ids),1)}%)")

if len(overlap) < len(page0_ids) * 0.5:
    print("PAGINATION WORKS! Let's count unique products across pages")
    all_ids = set(page0_ids) | set(page1_ids)
    
    # Try page 2
    r = requests.get(url, headers=H, params={**base_params, "offset": 2000}, timeout=60)
    data = r.json()
    page2_ids = [p["id"] for p in data.get("data", [])]
    print(f"Page 2 (offset=2000): {len(page2_ids)} products")
    overlap2 = set(page2_ids) & all_ids
    print(f"  Overlap with previous: {len(overlap2)}")
    all_ids |= set(page2_ids)
    print(f"  Total unique so far: {len(all_ids)}")
else:
    print("PAGINATION BROKEN - offset is ignored")
    # Try 'page' param instead
    print("\nTrying 'page' parameter...")
    r = requests.get(url, headers=H, params={**base_params, "page": 2}, timeout=60)
    data = r.json()
    page_ids = [p["id"] for p in data.get("data", [])]
    overlap_p = set(page0_ids) & set(page_ids)
    print(f"  page=2: {len(page_ids)} products, overlap: {len(overlap_p)}")

    # Try 'start' param
    r = requests.get(url, headers=H, params={**base_params, "start": 1000}, timeout=60)
    data = r.json()
    start_ids = [p["id"] for p in data.get("data", [])]
    overlap_s = set(page0_ids) & set(start_ids)
    print(f"  start=1000: {len(start_ids)} products, overlap: {len(overlap_s)}")

    # Try 'from' param
    r = requests.get(url, headers=H, params={**base_params, "from": 1000}, timeout=60)
    data = r.json()
    from_ids = [p["id"] for p in data.get("data", [])]
    overlap_f = set(page0_ids) & set(from_ids)
    print(f"  from=1000: {len(from_ids)} products, overlap: {len(overlap_f)}")
