#!/usr/bin/env python3
"""Test JD Sports Awin CSV feed."""
import requests
import gzip
import io
import csv

url = "https://productdata.awin.com/datafeed/download/apikey/489bdc79c6362be4f7d60e6b2506a2de/language/nl/cid/139,614,189,194/fid/77079/rid/0/hasEnhancedFeeds/0/columns/aw_deep_link,product_name,aw_product_id,merchant_product_id,merchant_image_url,description,merchant_category,search_price,merchant_name,merchant_id,category_name,category_id,aw_image_url,currency,store_price,delivery_cost,merchant_deep_link,language,last_updated,display_price,data_feed_id,commission_group,merchant_product_category_path,merchant_product_second_category,merchant_product_third_category,brand_name,brand_id,colour,product_short_description,specifications,condition,product_model,model_number,dimensions,keywords,promotional_text,product_type,merchant_thumb_url,large_image,alternate_image,aw_thumb_url,alternate_image_two,alternate_image_three,alternate_image_four,ean,isbn,upc,mpn,parent_product_id,product_GTIN,basket_link,Fashion%3Asuitable_for,Fashion%3Acategory,Fashion%3Asize,Fashion%3Amaterial,Fashion%3Apattern,Fashion%3Aswatch/format/csv/delimiter/%2C/compression/gzip/adultcontent/1/"

print("Downloading JD Sports feed...")
r = requests.get(url, timeout=120, stream=True)
print(f"Status: {r.status_code}, Content-Type: {r.headers.get('content-type')}")

if r.status_code == 200:
    # Decompress gzip
    data = gzip.decompress(r.content)
    text = data.decode('utf-8', errors='replace')
    reader = csv.DictReader(io.StringIO(text))
    
    rows = []
    for i, row in enumerate(reader):
        rows.append(row)
        if i >= 9:
            break
    
    print(f"\nTotal bytes: {len(data):,}")
    print(f"Columns: {list(rows[0].keys()) if rows else 'none'}")
    print(f"\nFirst 3 products:")
    for row in rows[:3]:
        print(f"  {row.get('brand_name', '?')} - {row.get('product_name', '?')[:80]}")
        print(f"    Price: {row.get('search_price')} {row.get('currency')}")
        print(f"    Category: {row.get('merchant_category', '')[:60]}")
        print(f"    Model: {row.get('product_model', '')}")
        print(f"    Size: {row.get('Fashion:size', '')}")
        print(f"    EAN: {row.get('ean', '')}")
        print(f"    Color: {row.get('colour', '')}")
        print()
    
    # Count total lines
    total = text.count('\n') - 1
    print(f"Total products in feed: ~{total:,}")
    
    # Count sneaker brands
    sneaker_brands = {'nike', 'adidas', 'new balance', 'puma', 'asics', 'jordan', 'reebok', 'converse', 'vans', 'saucony', 'hoka', 'on running', 'salomon'}
    brand_counts = {}
    reader2 = csv.DictReader(io.StringIO(text))
    for row in reader2:
        b = (row.get('brand_name') or '').lower()
        brand_counts[b] = brand_counts.get(b, 0) + 1
    
    print("Top 15 brands:")
    for b, c in sorted(brand_counts.items(), key=lambda x: -x[1])[:15]:
        marker = " *sneaker*" if b in sneaker_brands else ""
        print(f"  {b}: {c}{marker}")
