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

# Try manify campaign with cursor pagination
cid = 1622230
url = f'{WEBGAINS_API}/auth/publishers/{PUBLISHER_ID}/campaigns/{cid}/feeds/products'

cursor = None
total = 0
brands = {}
for page in range(50):
    params = {'size': 1000, 'fields[]': ['title','brand','price','id','ean','colour','category_name','model_number','image_link','link','description','rrp','currency','in_stock']}
    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 += 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}, cursor: {cursor[:30]}...')
    if len(products) < 1000:
        break

print(f'\nTotal: {total}')
print('Top brands:')
for b, c in sorted(brands.items(), key=lambda x: -x[1])[:30]:
    print(f'  {b:25}: {c}')
