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

# Check feed details for download URLs
cid = 1622230
url = f'{WEBGAINS_API}/auth/publishers/{PUBLISHER_ID}/campaigns/{cid}/feeds'
r = requests.get(url, headers=webgains_headers(), params={'size': 100})
d = r.json()
for f in d.get('data', []):
    count = f.get('products_successfully_stored', 0) or 0
    if count > 0:
        # Print all fields to find download URL
        name = f.get('name', '')
        fid = f['id']
        print(f'\nFeed {fid}: {name} ({count} products)')
        # Check for downloadable URL fields
        for key in sorted(f.keys()):
            if key not in ['program', 'name', 'id', 'products_successfully_stored']:
                val = f[key]
                if val and str(val).strip():
                    print(f'  {key}: {str(val)[:200]}')
        if 'program' in f and f['program']:
            for key in sorted(f['program'].keys()):
                val = f['program'][key]
                if val and str(val).strip():
                    print(f'  program.{key}: {str(val)[:200]}')
        break  # Just check first one for structure

# Also try Awin API
print('\n\n=== AWIN API ===')
awin_token = 'bebd7839-013a-434d-a3da-7db2ecefb6b1'
awin_headers = {'Authorization': f'Bearer {awin_token}'}
# List publishers/programmes
r2 = requests.get('https://api.awin.com/publishers', headers=awin_headers)
print(f'Awin publishers: {r2.status_code}')
if r2.ok:
    print(r2.text[:500])

# Try product search
r3 = requests.get('https://productdata.awin.com/datafeed/list/apikey/bebd7839-013a-434d-a3da-7db2ecefb6b1', timeout=30)
print(f'\nAwin product feeds list: {r3.status_code}')
if r3.ok:
    print(r3.text[:1000])
