#!/usr/bin/env python3
"""Capture visuelle de la page SneakSafe pour diagnostic"""
import asyncio
from playwright.async_api import async_playwright

async def main():
    async with async_playwright() as p:
        browser = await p.chromium.connect_over_cdp("http://localhost:9222")
        context = browser.contexts[0] if browser.contexts else await browser.new_context(viewport={"width": 1920, "height": 1080})
        page = await context.new_page()

        print("Navigation vers la page SneakSafe...")
        await page.goto("https://www.ysenra.fr/products/sneaksafe-sac-lavage-chaussures", wait_until="networkidle", timeout=30000)
        await page.wait_for_timeout(2000)

        # Capture complète de la page
        print("Capture de la page complète...")
        await page.screenshot(path="C:\\ASSISTANT_IA\\artifacts\\SNEAKSAFE_PAGE_COMPLETE_29102026.png", full_page=True)

        # Capture du hero
        print("Capture de la section hero...")
        await page.screenshot(path="C:\\ASSISTANT_IA\\artifacts\\SNEAKSAFE_HERO_29102026.png", clip={"x": 0, "y": 0, "width": 1920, "height": 1080})

        # Vérifier les images chargées
        print("\nVérification des images chargées...")
        images = await page.query_selector_all("img")
        failed_images = []
        for i, img in enumerate(images[:20]):  # Limiter aux 20 premières
            src = await img.get_attribute("src")
            natural_width = await img.evaluate("el => el.naturalWidth")
            if natural_width == 0:
                failed_images.append(src)

        if failed_images:
            print(f"\n❌ {len(failed_images)} images non chargées:")
            for src in failed_images:
                print(f"  - {src}")
        else:
            print(f"\n✅ Toutes les images principales chargées ({len(images[:20])} vérifiées)")

        # Chercher la section Trio
        print("\nRecherche de la section Trio Stratégique...")
        trio_section = await page.query_selector("text=Trio Stratégique")
        if trio_section:
            print("✅ Section Trio Stratégique trouvée")
            await trio_section.screenshot(path="C:\\ASSISTANT_IA\\artifacts\\SNEAKSAFE_TRIO_SECTION_29102026.png")
        else:
            print("❌ Section Trio Stratégique NON trouvée")

        print("\nCaptures sauvegardées dans C:\\ASSISTANT_IA\\artifacts\\")
        await browser.close()

if __name__ == "__main__":
    asyncio.run(main())
