import requests
import sys
sys.path.insert(0, r'C:\Users\Roel\clawd\scripts\demandgen-pipeline')
from config import WEBGAINS_API, PUBLISHER_ID, webgains_headers

# Test ladify campaign with specific feeds
cid = 1622250
url = f'{WEBGAINS_API}/auth/publishers/{PUBLISHER_ID}/campaigns/{cid}/feeds/products'

# Try Sneakermood feed (16158) via ladify
params = {'size': 5, 'feeds[]': [16158], 'fields[]': ['title','brand','price','id']}
r = requests.get(url, headers=webgains_headers(), params=params)
d = r.json()
print(f'Sneakermood via ladify: {d.get("pagination",{}).get("total")} products')
for p in d.get('data', []):
    print(f'  {p.get("brand",""):15} | {p.get("title","")[:60]}')

# Let's paginate through ladify to count unique products
print('\n--- Paginating through all ladify products ---')
cursor = None
total_fetched = 0
brands = {}
for page in range(20):  # 20 pages of 1000 = 20k max
    params = {'size': 1000, 'fields[]': ['title','brand','price','id']}
    if cursor:
        params['search_after'] = cursor
    r = requests.get(url, headers=webgains_headers(), params=params)
    d = r.json()
    products = d.get('data', [])
    if not products:
        break
    new_cursor = d.get('pagination', {}).get('search_cursor', '')
    if new_cursor == cursor:
        break
    cursor = new_cursor
    total_fetched += len(products)
    for p in products:
        b = p.get('brand', 'Unknown')
        brands[b] = brands.get(b, 0) + 1
    print(f'  Page {page+1}: {len(products)} products, total: {total_fetched}, cursor: {cursor}')
    if len(products) < 1000:
        break

print(f'\nTotal fetched: {total_fetched}')
print('Brands:')
for b, c in sorted(brands.items(), key=lambda x: -x[1])[:20]:
    print(f'  {b:20}: {c}')
