#!/usr/bin/env python3
"""Discover Awin feeds for sneaker shops."""
import requests
import json

TOKEN = 'bebd7839-013a-434d-a3da-7db2ecefb6b1'
PUB_ID = '374301'
BASE = 'https://productdata.awin.com'

# 1. List all advertisers we're joined with
print("=== Awin Advertisers ===")
r = requests.get(f'https://api.awin.com/publishers/{PUB_ID}/programmes?relationship=joined',
                 headers={'Authorization': f'Bearer {TOKEN}'}, timeout=30)
print(f'Status: {r.status_code}')
if r.status_code == 200:
    programs = r.json()
    print(f'Total joined programmes: {len(programs)}')
    for p in programs:
        name = p.get('name', '?')
        pid = p.get('id', '?')
        keywords = ['sneaker', 'shoe', 'schoen', 'sport', 'nike', 'adidas', 'puma', 'asics',
                    'foot', 'jd ', 'zalando', 'snipes', 'size?', 'otrium', 'omoda']
        is_relevant = any(kw in name.lower() for kw in keywords)
        if is_relevant:
            print(f'  *** [{pid}] {name}')
        # Print all to see what's available
    # Print all
    for p in programs:
        print(f'  [{p.get("id")}] {p.get("name")}')
else:
    print(f'Error: {r.text[:500]}')

# 2. List available product feeds/datafeeds
print("\n=== Awin Datafeeds ===")
r2 = requests.get(f'{BASE}/feeds/list?publisherId={PUB_ID}',
                  headers={'Authorization': f'Bearer {TOKEN}'}, timeout=30)
print(f'Status: {r2.status_code}')
if r2.status_code == 200:
    try:
        feeds = r2.json()
        print(f'Total feeds: {len(feeds)}')
        for f in feeds[:50]:
            print(f'  [{f.get("advertiserId")}] {f.get("advertiserName", "?")} - {f.get("feedName", "?")} [{f.get("productCount", "?")} products]')
    except:
        print(f'Response (not JSON): {r2.text[:1000]}')
else:
    print(f'Error: {r2.text[:500]}')

# 3. Try the product search endpoint  
print("\n=== Awin Product Search ===")
r3 = requests.get(f'{BASE}/product-feeds?publisherId={PUB_ID}',
                  headers={'Authorization': f'Bearer {TOKEN}'}, timeout=30)
print(f'Status: {r3.status_code}')
if r3.status_code == 200:
    print(f'Response: {r3.text[:2000]}')
else:
    print(f'Error: {r3.text[:500]}')

# 4. Try getting feeds via commission groups/programmes endpoint
print("\n=== Trying product list endpoint ===")
r4 = requests.get(f'https://api.awin.com/publishers/{PUB_ID}/product-feeds',
                  headers={'Authorization': f'Bearer {TOKEN}'}, timeout=30)
print(f'Status: {r4.status_code}')
if r4.status_code == 200:
    print(f'Response: {r4.text[:2000]}')
else:
    print(f'Error: {r4.text[:500]}')
