#!/usr/bin/env python3
"""Test du parser Backmarket avec un Samsung Galaxy S23."""

from pricewatch.app.scraping.pw_fetch import fetch_playwright
from pricewatch.app.stores.backmarket.store import BackmarketStore
import json

# URL d'un Samsung Galaxy S23 reconditionné
url = "https://www.backmarket.fr/fr-fr/p/samsung-galaxy-s23"

print("=" * 80)
print("TEST BACKMARKET - SAMSUNG GALAXY S23")
print("=" * 80)
print(f"\nURL: {url}")

# Fetch avec Playwright (obligatoire pour Backmarket)
print("\n[1/3] Récupération de la page avec Playwright...")
result = fetch_playwright(url, headless=True, timeout_ms=60000)

if not result.success:
    print(f"❌ ÉCHEC du fetch: {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 le HTML
html_file = "scraped/backmarket_samsung_pw.html"
with open(html_file, "w", encoding="utf-8") as f:
    f.write(result.html)
print(f"✓ HTML sauvegardé: {html_file}")

# Parser avec BackmarketStore
print("\n[2/3] Parsing avec BackmarketStore...")
store = BackmarketStore()
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]):
        print(f"  [{i+1}] {img[:80]}...")

print(f"\nCategory: {snapshot.category or 'Non extraite'}")
print(f"Specs: {len(snapshot.specs)} caractéristiques")
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: {snapshot.is_complete()}")

# Export JSON
json_file = "scraped/backmarket_samsung_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)
