import json
import os

# CONFIGURAZIONE
SOURCE_FILE = "meta_Clothing_Shoes_and_Jewelry.jsonl" 
OUTPUT_FILE = "metadata_cleaned_final.json"
TARGET_COUNT = 100000 # Prendiamo 100k prodotti per la demo

def process_metadata():
    print(f"--- PULIZIA METADATI: {SOURCE_FILE} ---")
    
    if not os.path.exists(SOURCE_FILE):
        print(f"❌ Errore: File {SOURCE_FILE} non trovato!")
        return

    count_saved = 0
    clean_products = [] 

    with open(SOURCE_FILE, 'r', encoding='utf-8') as f_in:
        for i, line in enumerate(f_in):
            try:
                data = json.loads(line)
                
                # 1. Filtri Base
                asin = data.get('asin') or data.get('parent_asin')
                title = data.get('title')
                
                if not asin or not title: continue
                
                # 2. Recupero Immagine (Fondamentale per la Tesi e Visual Rec)
                image_url = ""
                if 'images' in data and data['images']:
                    # Cerca l'immagine più grande
                    img_entry = data['images'][0]
                    image_url = img_entry.get('large', '') or img_entry.get('hi_res', '') or img_entry.get('thumb', '')
                elif 'imUrl' in data:
                    image_url = data['imUrl']

                # Salviamo
                if len(title) > 3:
                    clean_products.append({
                        "asin": asin,
                        "title": title,
                        "brand": data.get('brand', 'Generic'),
                        "price": data.get('price', 'N/A'),
                        "categories": data.get('categories', []),
                        "image_url": image_url 
                    })
                    count_saved += 1
                    
                if count_saved >= TARGET_COUNT: break
                
                if i % 50000 == 0: print(f"-> Scansionati {i} prodotti...")

            except: continue

    print(f"-> Salvataggio di {len(clean_products)} prodotti puliti...")
    with open(OUTPUT_FILE, 'w', encoding='utf-8') as f_out:
        json.dump(clean_products, f_out, indent=2)
        
    print(f"✅ FATTO! Creato {OUTPUT_FILE}")

if __name__ == "__main__":
    process_metadata()