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 different queries across different campaigns to get variety
campaigns = [1622230, 1622250]
queries = ['Nike', 'adidas', 'New Balance', 'Puma', 'Jordan', 'Converse', 'Vans', 'ASICS', 'Reebok', 'sneaker', 'schoen']

all_products = {}
for cid in campaigns:
    url = f'{WEBGAINS_API}/auth/publishers/{PUBLISHER_ID}/campaigns/{cid}/feeds/products'
    for q in queries:
        params = {
            'size': 100,
            'q': q,
            'fields[]': ['title','brand','price','id','ean','colour','category_name','model_number','image_link','link','description','rrp','currency','in_stock']
        }
        r = requests.get(url, headers=webgains_headers(), params=params)
        if not r.ok:
            continue
        d = r.json()
        products = d.get('data', [])
        new = 0
        for p in products:
            pid = str(p.get('id', ''))
            if pid and pid not in all_products:
                all_products[pid] = p
                new += 1
        print(f'Campaign {cid}, q={q:15}: {len(products)} results, {new} new (total unique: {len(all_products)})')

print(f'\nTotal unique products: {len(all_products)}')
brands = {}
for p in all_products.values():
    b = p.get('brand', 'Unknown')
    brands[b] = brands.get(b, 0) + 1
print('Brands:')
for b, c in sorted(brands.items(), key=lambda x: -x[1])[:20]:
    print(f'  {b:25}: {c}')
