#!/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.launch(headless=True)
        page = await browser.new_page(viewport={"width": 1920, "height": 1080})

        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(3000)

        # 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[:30]):  # Limiter aux 30 premières
            src = await img.get_attribute("src")
            natural_width = await img.evaluate("el => el.naturalWidth")
            if natural_width == 0 and src:
                failed_images.append(src)

        if failed_images:
            print(f"\n❌ {len(failed_images)} images non chargées:")
            for src in failed_images[:10]:
                print(f"  - {src}")
        else:
            print(f"\n✅ Toutes les images principales chargées ({len(images[:30])} 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")
            # Scroll jusqu'à elle
            await trio_section.scroll_into_view_if_needed()
            await page.wait_for_timeout(1000)
            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())
