#!/usr/bin/env python3
"""Check what Van Arendonk rows WITHOUT item_group_id look like, and Foot Locker name patterns."""
import requests, csv, io, gzip
from collections import Counter

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"

# Van Arendonk — check rows without item_group_id
print("=== Van Arendonk: rows without item_group_id ===")
url = "https://platform-api.webgains.com/auth/publishers/1347930/campaigns/1622230/feeds/products"
r = requests.get(url, headers={"Authorization": f"Bearer {TOKEN}"}, params={"feedIds[]": "17108", "format": "csv"}, timeout=300)
reader = csv.DictReader(io.StringIO(r.text))
rows = list(reader)
no_ig = [row for row in rows if not (row.get('item_group_id') or '').strip()]
has_ig = [row for row in rows if (row.get('item_group_id') or '').strip()]
print(f"Total: {len(rows)}, with item_group_id: {len(has_ig)}, without: {len(no_ig)}")

# Show some without item_group_id
for row in no_ig[:5]:
    print(f"  title: {row.get('title','')[:80]}")
    print(f"  brand: {row.get('brand','')}, id: {row.get('id','')}, size: {row.get('size','')}")
    print(f"  gtin: {row.get('gtin','')}, item_group_id: '{row.get('item_group_id','')}'")
    print()

# Check if no-ig rows have different names for same product
from collections import defaultdict
by_brand_base = defaultdict(list)
for row in no_ig[:500]:
    brand = (row.get('brand') or '').lower()
    title = (row.get('title') or '').strip()
    by_brand_base[f"{brand}||{title.lower()}"].append(row)

dupes = {k: v for k, v in by_brand_base.items() if len(v) > 1}
print(f"Name-duplicated groups (no item_group_id): {len(dupes)}")
for k, v in list(dupes.items())[:3]:
    print(f"  '{k}': {len(v)} rows, ids: {[r.get('id','') for r in v]}")

# Foot Locker — check name patterns
print("\n=== Foot Locker: name patterns ===")
fl_url = "https://productdata.awin.com/datafeed/download/apikey/489bdc79c6362be4f7d60e6b2506a2de/fid/29905/format/csv/language/nl/delimiter/%2C/compression/gzip/columns/data_feed_id%2Cmerchant_id%2Cmerchant_name%2Caw_product_id%2Caw_deep_link%2Caw_image_url%2Caw_thumb_url%2Ccategory_id%2Ccategory_name%2Cbrand_id%2Cbrand_name%2Cmerchant_product_id%2Cmerchant_category%2Cean%2Cmpn%2Cisbn%2Cmodel_number%2Cproduct_name%2Cdescription%2Cspecifications%2Clanguage%2Cmerchant_deep_link%2Cmerchant_thumb_url%2Cmerchant_image_url%2Cdelivery_time%2Cvalid_from%2Cvalid_to%2Ccurrency%2Csearch_price%2Cstore_price%2Cdelivery_cost%2Cweb_offer%2Cpre_order%2Cin_stock%2Cstock_quantity%2Cwarranty%2Ccondition%2Cparent_product_id%2Ccommission_group%2Clast_updated%2Cdimensions%2Ccolour%2Ckeywords%2Ccustom_1%2Ccustom_2%2Csaving%2CFashion%3Asuitable_for%2CFashion%3Asize%2CFashion%3Amaterial%2CFashion%3Apattern%2CFashion%3Aswatch%2Crating%2Calternate_image%2Clarge_image%2Cbasket_link%2Cproduct_short_description%2Cmerchant_product_category_path%2Cmerchant_product_second_category%2Cmerchant_product_third_category%2Csavings_percent%2Cproduct_price_old%2Calternate_image_two%2Calternate_image_three%2Calternate_image_four/"
r2 = requests.get(fl_url, timeout=300)
fl_rows = list(csv.DictReader(io.StringIO(gzip.decompress(r2.content).decode('utf-8', errors='replace'))))

# Check parent_product_id usage
has_ppid = sum(1 for r in fl_rows if (r.get('parent_product_id') or '').strip())
print(f"Total: {len(fl_rows)}, with parent_product_id: {has_ppid}")

# Sample names
for row in fl_rows[:5]:
    print(f"  name: {row.get('product_name','')[:100]}")
    print(f"  parent_product_id: {row.get('parent_product_id','')}")
    print(f"  Fashion:size: {row.get('Fashion:size','')}")
    print()

# Check for dupe parent_product_ids
ppid_counts = Counter((r.get('parent_product_id') or '').strip() for r in fl_rows if (r.get('parent_product_id') or '').strip())
top_ppids = ppid_counts.most_common(5)
print(f"Top parent_product_id groups:")
for ppid, cnt in top_ppids:
    print(f"  {ppid}: {cnt} rows")
    # Show names in this group
    group = [r for r in fl_rows if r.get('parent_product_id','').strip() == ppid][:3]
    for g in group:
        print(f"    name: {g.get('product_name','')[:80]}, size: {g.get('Fashion:size','')}")
