import asyncio
import json
import os
import sys

# Ajout du chemin pour retrouver les modules internes du projet
sys.path.append(os.path.abspath(os.path.join(os.path.dirname(__file__), '../..')))

from Harken.API.tokens import access_tokens  # Liste de tokens d'accès Shopify
from Harken.API.get_all_products import get_all_products  # Fonction utilitaire déjà fournie

# --- Paramètres de configuration ---
SHOP_NAME = "https://harkenb2b.myshopify.com"  # URL du shop (sans slash final)
API_VERSION = "2024-10"  # Version de l'API Shopify
TOKENS = access_tokens  # Gardé sous forme de liste pour compatibilité avec get_all_products
# -----------------------------------


async def main() -> None:
    """Récupère tous les produits et affiche combien possèdent au moins une image."""

    # 1. Récupération des produits (JSON sérialisé) via la fonction utilitaire existante
    print("⏳ Récupération de la liste des produits Shopify…")
    products_json: str = await get_all_products(TOKENS, SHOP_NAME, API_VERSION)

    # 2. Désérialisation JSON ➜ dict
    products_data: dict = json.loads(products_json)
    products: list = products_data.get("products", [])

    # 3. Calculs
    total_products: int = len(products)
    products_with_images: int = sum(
        1 for product in products if product.get("images")  # "images" est non‑vide
    )

    # 4. Affichage
    print("📊 Statistiques des images :")
    print(f" • Nombre total de produits   : {total_products}")
    print(f" • Produits avec ≥1 image      : {products_with_images}")

    if total_products:
        ratio = (products_with_images / total_products) * 100
        print(f" • Pourcentage couvert         : {ratio:.1f}%")
    else:
        print("Aucun produit trouvé !")


if __name__ == "__main__":
    # Exécution asynchrone
    asyncio.run(main())
