#!/usr/bin/env python3
"""Try the products endpoint with feed filter - this is what feed-sync-v3 uses."""
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",
}

CAMP_ID = "1622230"

# The working endpoint from feed-sync-v3 - fetch products filtered by specific feed
# Baskets feed = 18588, Sneakermood = 16158
for feed_id, name in [("18588", "Baskets"), ("16158", "Sneakermood")]:
    url = f"{WEBGAINS_API}/auth/publishers/{PUBLISHER_ID}/campaigns/{CAMP_ID}/feeds/products"
    params = {
        "feeds[]": [feed_id],
        "size": 2,
        "fields[]": ["title", "brand", "link", "price", "google_product_category", "product_type", "gtin", "size", "colour", "id"],
    }
    print(f"\n=== Feed {name} ({feed_id}) ===")
    r = requests.get(url, headers=HEADERS, params=params, timeout=15)
    print(f"Status: {r.status_code}")
    data = r.json()
    products = data.get("data", [])
    print(f"Products returned: {len(products)}")
    for p in products:
        print(f"\n  Title: {p.get('title')}")
        print(f"  Brand: {p.get('brand')}")
        print(f"  Price: {p.get('price')}")
        print(f"  GPC: {p.get('google_product_category')}")
        print(f"  Product Type: {p.get('product_type')}")
        print(f"  GTIN: {p.get('gtin')}")
        print(f"  Size: {p.get('size')}")
        print(f"  Link: {str(p.get('link', ''))[:120]}")
        # Check which wgprogramid is in the link
        link = p.get('link', '')
        if 'wgprogramid=' in str(link):
            pgm = str(link).split('wgprogramid=')[1].split('&')[0]
            print(f"  → wgprogramid in link: {pgm}")
