#!/usr/bin/env python3
"""Get CSV feed download URLs from Webgains API for each program/merchant."""
import requests
import json

WEBGAINS_API = "https://platform-api.webgains.com"
PUBLISHER_ID = "1347930"
TOKEN = "eyJ0eXAiOiJKV1QiLCJhbGciOiJSUzI1NiJ9.eyJhdWQiOiIxMDAwMzEiLCJqdGkiOiIwNjM5ODEwYzQ0YzM1MTBlZmZiMGRkYWNiM2EyNWFlNTg2YzFmNmQ5ODNjNGQ2MDA4NzkwZmM0NTkzY2EyNjg4ZGViZDE4NTgwOThmYjJjYiIsImlhdCI6MTc3MTUxNjc4OS44MzAxMzUsIm5iZiI6MTc3MTUxNjc4OS44MzAxMzcsImV4cCI6MjA4NzA0OTU4OS44MjcwODgsInN1YiI6IjIwMTcwOCIsInNjb3BlcyI6W119.ZxlCDsyBI7_SNUWjO-PUxDYKvQVfuUBzU5n-SgqHdAZCnOj9253yVcJIS5NRXJLrnyx0E_9bn8ymfXS71cvPISA4hPAlKCSMjdkv9f2k2FFwdAlQQ6mdhBB2m9dzHUIn34QwA8s4-ikB1ATKIZeqiJL-3Gh9eiMYPpvaJdCCjN-oGPMhhlehkgA04cZSgwy6HfKgb6uPvUSG9w_tvN8U06uEHKV11DedQHNWWGzuXpd2FEL3baV0z9KjSNMZsJZHx2yHUb6gVWJwaR7_louahMvrPXG-YLgnGvNMcPDPjm-z5HJ-JKcPv9_FBqAYGItZxgeGgVoS8MVhW6lc1a-6CvYtVanfXF42MPPNP-Qq76qRtHPLRBdkIATMuBxVJrMzlprT3n4F0sUUltGR4ixZuzMUm5LeyPMHgmjy2UfYzsl9eVqx0oJCmKUyY9maBXwFLJa8J1NHM_nWp3hPpCGkMyhzHK1DIf_z8Wirg1hp5vKts93eERWJBlQwhucUUS_KnZeC_gVaQ7kT4jCwaEWkpPxPxyXWRoK0mcYS_JGz3JbpVhRLj5DVYIB9k54DXqjYlCx8ezJZE9sf2SX3VWvPvD1xSQD1caTCm0To0B-MkeU1YpEfWdUslpCJXrpOy2Wbs8iDHR1BzNrMnOEr4zbz_EoB4JJe3tGTGjzNLNIw6XI"

HEADERS = {
    "Authorization": f"Bearer {TOKEN}",
    "Content-Type": "application/json",
    "Accept": "application/json",
}

CAMPAIGNS = {
    "manify.nl": "1622230",
    "ladify.nl": "1622250", 
    "thehike.nl": "1622240",
    "mellowed.nl": "1622260",
}

# Get all feeds with their download URLs
for site, camp_id in CAMPAIGNS.items():
    print(f"\n=== {site} (campaign {camp_id}) ===")
    
    # Get feeds
    url = f"{WEBGAINS_API}/auth/publishers/{PUBLISHER_ID}/campaigns/{camp_id}/feeds"
    r = requests.get(url, headers=HEADERS, params={"size": 100}, timeout=30)
    r.raise_for_status()
    feeds = r.json().get("data", [])
    
    for f in feeds:
        prog = f.get("program", {})
        print(f"\nFeed: {f.get('name')}")
        print(f"  Program: {prog.get('name')} (id={prog.get('id')})")
        print(f"  Products: {f.get('products_successfully_stored', 0)}")
        print(f"  Feed ID: {f.get('id')}")
        
        # Check for download URL
        for key in ['url', 'download_url', 'feed_url', 'csv_url', 'link']:
            if f.get(key):
                print(f"  {key}: {f[key]}")
        
        # Print all keys to find the URL field
        print(f"  All keys: {list(f.keys())}")
        
        # Print full feed details for first one
    
    if feeds:
        print(f"\n--- Full first feed object ---")
        print(json.dumps(feeds[0], indent=2, default=str))
    
    # Only check first campaign
    break
