#!/usr/bin/env python3
"""Détail complet du produit Backmarket extrait."""

from pricewatch.app.stores.backmarket.store import BackmarketStore
import json

# Charger la fixture
with open("scraped/backmarket_pw.html", "r", encoding="utf-8") as f:
    html = f.read()

url = "https://www.backmarket.fr/fr-fr/p/iphone-15-pro"

store = BackmarketStore()
snapshot = store.parse(html, url)

print("=" * 80)
print("DÉTAIL COMPLET DU PRODUIT - BACKMARKET")
print("=" * 80)

# IDENTITÉ
print("\n📱 IDENTITÉ DU PRODUIT")
print("-" * 80)
print(f"Nom: {snapshot.title}")
print(f"Store: {snapshot.source.upper()}")
print(f"Référence (SKU): {snapshot.reference}")
print(f"URL: {snapshot.url}")

# PRIX
print("\n💰 INFORMATIONS PRIX")
print("-" * 80)
print(f"Prix: {snapshot.price} {snapshot.currency}")
if snapshot.shipping_cost:
    print(f"Frais de port: {snapshot.shipping_cost} {snapshot.currency}")
else:
    print(f"Frais de port: Non spécifié")

# DISPONIBILITÉ
print("\n📦 DISPONIBILITÉ")
print("-" * 80)
print(f"Stock: {snapshot.stock_status}")
print(f"Date d'extraction: {snapshot.fetched_at.strftime('%Y-%m-%d %H:%M:%S')}")

# IMAGES
print("\n🖼️  IMAGES")
print("-" * 80)
print(f"Nombre d'images: {len(snapshot.images)}")
if snapshot.images:
    for i, img in enumerate(snapshot.images[:5], 1):
        print(f"  [{i}] {img}")
    if len(snapshot.images) > 5:
        print(f"  ... et {len(snapshot.images) - 5} autres images")
else:
    print("  Aucune image extraite")

# CATÉGORIE
print("\n📂 CATÉGORIE")
print("-" * 80)
if snapshot.category:
    print(f"Catégorie: {snapshot.category}")
else:
    print("Catégorie: Non extraite")

# CARACTÉRISTIQUES
print("\n🔧 CARACTÉRISTIQUES TECHNIQUES")
print("-" * 80)
if snapshot.specs:
    print(f"Nombre de caractéristiques: {len(snapshot.specs)}")
    for key, value in snapshot.specs.items():
        print(f"  • {key}: {value}")
else:
    print("Aucune caractéristique technique extraite")

# DEBUG
print("\n🔍 INFORMATIONS DE DEBUG")
print("-" * 80)
print(f"Méthode de récupération: {snapshot.debug.method}")
print(f"Status du parsing: {snapshot.debug.status}")
print(f"Complet: {'✓ OUI' if snapshot.is_complete() else '✗ NON'}")

if snapshot.debug.errors:
    print(f"\n⚠️  Erreurs ({len(snapshot.debug.errors)}):")
    for err in snapshot.debug.errors:
        print(f"  • {err}")

if snapshot.debug.notes:
    print(f"\n📝 Notes ({len(snapshot.debug.notes)}):")
    for note in snapshot.debug.notes:
        print(f"  • {note}")

if snapshot.debug.duration_ms:
    print(f"\nDurée de récupération: {snapshot.debug.duration_ms}ms")

if snapshot.debug.html_size_bytes:
    print(f"Taille HTML: {snapshot.debug.html_size_bytes:,} bytes")

# EXPORT JSON
print("\n" + "=" * 80)
print("EXPORT JSON")
print("=" * 80)

json_data = snapshot.to_dict()
json_str = json.dumps(json_data, indent=2, ensure_ascii=False)

# Sauvegarder
output_file = "scraped/backmarket_iphone15pro_detail.json"
with open(output_file, "w", encoding="utf-8") as f:
    f.write(json_str)

print(f"✓ Détail complet sauvegardé: {output_file}")
print(f"Taille du JSON: {len(json_str):,} caractères")

# Afficher un extrait
lines = json_str.split('\n')
print(f"\nExtrait (30 premières lignes):")
print("-" * 80)
for line in lines[:30]:
    print(line)
if len(lines) > 30:
    print(f"... et {len(lines) - 30} lignes supplémentaires")

print("\n" + "=" * 80)
print("FIN DU RAPPORT")
print("=" * 80)
