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

# ladify campaign showed sneaker products, let's explore it
cid = 1622250
url = f'{WEBGAINS_API}/auth/publishers/{PUBLISHER_ID}/campaigns/{cid}/feeds/products'

# Get first 10 products
params = {'size': 10, 'fields[]': ['title','brand','price','id','category_name','image_link','link','ean','colour','model_number','in_stock','rrp','currency','description']}
r = requests.get(url, headers=webgains_headers(), params=params)
d = r.json()
print(f'Total: {d.get("pagination", {}).get("total")}')
print(f'Cursor: {d.get("pagination", {}).get("search_cursor")}')
for p in d.get('data', []):
    print(f'{p.get("brand",""):15} | {p.get("title","")[:60]:60} | {p.get("price",""):12} | {p.get("category_name","")}')
    
# Try cursor pagination
cursor = d.get('pagination', {}).get('search_cursor', '')
if cursor:
    print(f'\n--- Page 2 (cursor: {cursor}) ---')
    params2 = {'size': 5, 'search_after': cursor, 'fields[]': ['title','brand','price','id']}
    r2 = requests.get(url, headers=webgains_headers(), params=params2)
    d2 = r2.json()
    print(f'Cursor: {d2.get("pagination", {}).get("search_cursor")}')
    for p in d2.get('data', []):
        print(f'{p.get("brand",""):15} | {p.get("title","")[:60]}')
