#!/usr/bin/env python3
"""Test du 2ème produit AliExpress."""

from pricewatch.app.scraping.pw_fetch import fetch_playwright
from pricewatch.app.stores.aliexpress.store import AliexpressStore
import json

# URL du 2ème produit
url = "https://fr.aliexpress.com/item/1005009249035119.html"

print("=" * 80)
print("TEST ALIEXPRESS - 2ÈME PRODUIT")
print("=" * 80)
print(f"URL: {url}\n")

# Fetch avec Playwright + wait
print("[1/3] Récupération avec Playwright...")
result = fetch_playwright(
    url,
    headless=True,
    timeout_ms=15000,
    wait_for_selector=".product-title"
)

if not result.success:
    print(f"❌ ÉCHEC: {result.error}")
    exit(1)

print(f"✓ Page récupérée: {len(result.html):,} caractères")
print(f"  Durée: {result.duration_ms}ms")

# Sauvegarder
html_file = "scraped/aliexpress_product2_pw.html"
with open(html_file, "w", encoding="utf-8") as f:
    f.write(result.html)
print(f"✓ HTML sauvegardé: {html_file}")

# Parser
print("\n[2/3] Parsing avec AliexpressStore...")
store = AliexpressStore()
snapshot = store.parse(result.html, url)

# Afficher les résultats
print("\n[3/3] RÉSULTATS DU PARSING")
print("-" * 80)
print(f"Source: {snapshot.source}")
print(f"URL: {snapshot.url}")
print(f"Reference (SKU): {snapshot.reference}")
print(f"Title: {snapshot.title}")
print(f"Price: {snapshot.price} {snapshot.currency}")
print(f"Stock: {snapshot.stock_status}")
print(f"Images: {len(snapshot.images)} images")
if snapshot.images:
    for i, img in enumerate(snapshot.images[:3], 1):
        print(f"  [{i}] {img[:70]}...")

print(f"\nCategory: {snapshot.category or 'Non extraite'}")
print(f"Specs: {len(snapshot.specs)} specs")
if snapshot.specs:
    for key, value in list(snapshot.specs.items())[:5]:
        print(f"  • {key}: {value}")

print(f"\nDebug status: {snapshot.debug.status}")
print(f"Debug errors: {len(snapshot.debug.errors)}")
for err in snapshot.debug.errors:
    print(f"  ⚠️  {err}")

print(f"Debug notes: {len(snapshot.debug.notes)}")
for note in snapshot.debug.notes:
    print(f"  📝 {note}")

print(f"\nIs complete: {'✓ OUI' if snapshot.is_complete() else '✗ NON'}")

# Export JSON
json_file = "scraped/aliexpress_product2_detail.json"
json_data = snapshot.to_dict()
with open(json_file, "w", encoding="utf-8") as f:
    json.dump(json_data, f, indent=2, ensure_ascii=False)

print(f"\n✓ JSON sauvegardé: {json_file}")

print("\n" + "=" * 80)
if snapshot.is_complete():
    print("✅ TEST RÉUSSI - Parsing complet")
else:
    print("⚠️  TEST PARTIEL - Données manquantes")
print("=" * 80)
