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

# Test pagination with feedIds[]
print("=== Baskets (18588) - page 1 ===")
params = {"feedIds[]": [18588], "size": 1000, "offset": 0}
r = requests.get(url, headers=H, params=params, timeout=60)
data = r.json()
batch1 = data.get("data", [])
print(f"Got {len(batch1)} products")
print(f"First: {batch1[0].get('brand')} | {batch1[0].get('title','')[:50]}")
print(f"Last:  {batch1[-1].get('brand')} | {batch1[-1].get('title','')[:50]}")
first_ids = set(p.get("id") for p in batch1)

print("\n=== Baskets (18588) - page 2 ===")
params["offset"] = 1000
r2 = requests.get(url, headers=H, params=params, timeout=60)
data2 = r2.json()
batch2 = data2.get("data", [])
print(f"Got {len(batch2)} products")
if batch2:
    print(f"First: {batch2[0].get('brand')} | {batch2[0].get('title','')[:50]}")
    second_ids = set(p.get("id") for p in batch2)
    overlap = first_ids & second_ids
    print(f"Overlap with page 1: {len(overlap)} products")
    if overlap:
        print("⚠️ Pagination broken - returning duplicates")
else:
    print("No more products (total was ~{} from page 1)".format(len(batch1)))

# Quick check: different feeds return different data
print("\n=== Verify feed separation ===")
for fid, name in [(18588, "Baskets"), (16158, "Sneakermood"), (25578, "Patta")]:
    params = {"feedIds[]": [fid], "size": 3}
    r = requests.get(url, headers=H, params=params, timeout=10)
    data = r.json()
    prods = data.get("data", [])
    if prods:
        brands = set(p.get("brand") for p in prods)
        print(f"  Feed {fid} ({name}): {len(prods)} returned, brands={brands}")
        # Check affiliate link domain
        link = prods[0].get("link", "")
        if "baskets" in link.lower():
            print(f"    ✅ Link goes to Baskets")
        elif "patta" in link.lower():
            print(f"    ✅ Link goes to Patta")
        elif "sneakermood" in link.lower():
            print(f"    ✅ Link goes to Sneakermood")
        else:
            print(f"    Link: {link[:100]}")
    else:
        print(f"  Feed {fid} ({name}): NO DATA")
