#!/usr/bin/env python3
"""Discover ALL Webgains feeds across all campaigns."""
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}", "Accept": "application/json"}

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

EXISTING_FEED_IDS = {"18588", "16158", "25578"}  # Baskets, Sneakermood, Patta

for camp_id, camp_name in CAMPAIGNS.items():
    print(f"\n=== Campaign: {camp_name} ({camp_id}) ===")
    url = f"{WEBGAINS_API}/auth/publishers/{PUBLISHER_ID}/campaigns/{camp_id}/feeds"
    r = requests.get(url, headers=HEADERS, timeout=30)
    print(f"Status: {r.status_code}")
    if r.status_code == 200:
        feeds = r.json()
        if isinstance(feeds, dict):
            feeds = feeds.get('data', feeds.get('feeds', []))
        print(f"Total feeds: {len(feeds)}")
        for f in feeds:
            fid = str(f.get('id', '?'))
            fname = f.get('name', '?')
            merchant = f.get('merchant_name') or f.get('programName') or '?'
            pcount = f.get('product_count') or f.get('productCount') or '?'
            already = ' [ALREADY IMPORTED]' if fid in EXISTING_FEED_IDS else ''
            keywords = ['sneaker', 'shoe', 'schoen', 'sport', 'nike', 'adidas', 'puma',
                        'foot', 'kick', 'trainer', 'baskets', 'patta', 'snipes']
            is_relevant = any(kw in fname.lower() or kw in str(merchant).lower() for kw in keywords)
            marker = ' ***' if is_relevant and not already else ''
            print(f"  [{fid}] {fname} ({merchant}) [{pcount} products]{already}{marker}")
    else:
        print(f"Error: {r.text[:500]}")
