"""Visual QA capture: bigbreath.co localhost:8011 -- 2026-06-01"""
import asyncio, os, sys
from pathlib import Path
from playwright.async_api import async_playwright

BASE = "http://localhost:8011"
OUT  = Path("/home/sirdrez/arisingmedia-websites/bigbreath.co/.planning/qa/visual-2026-06-01")
OUT.mkdir(parents=True, exist_ok=True)

ROUTES = [
    ("home",        "/"),
    ("about",       "/about"),
    ("the-practice","/the-practice"),
    ("services",    "/services"),
    ("keynotes",    "/services/keynotes/"),
    ("testimonials","/testimonials"),
    ("gallery",     "/gallery"),
    ("contact",     "/contact"),
]

VIEWPORTS = [
    ("desktop", {"width": 1440, "height": 900}),
    ("mobile",  {"width": 390,  "height": 844}),
]

ERRORS = []

async def capture(page, url, slug, vp_name):
    try:
        await page.goto(url, wait_until="networkidle", timeout=30000)
        await asyncio.sleep(1.2)  # font swap
        fname = OUT / f"{slug}-{vp_name}.png"
        await page.screenshot(path=str(fname), full_page=True)
        print(f"OK  {fname.name}")
        return fname
    except Exception as e:
        msg = f"FAIL {slug}-{vp_name}: {e}"
        print(msg, file=sys.stderr)
        ERRORS.append(msg)
        return None

async def capture_dropdown(page, slug):
    """Desktop only: hover Services nav item and capture dropdown."""
    try:
        await page.goto(BASE + "/", wait_until="networkidle", timeout=30000)
        await asyncio.sleep(1.0)
        nav_item = page.locator("nav a", has_text="Services").first
        await nav_item.hover()
        await asyncio.sleep(0.8)
        fname = OUT / f"{slug}-dropdown-desktop.png"
        await page.screenshot(path=str(fname), full_page=False)
        print(f"OK  {fname.name}")
    except Exception as e:
        msg = f"FAIL {slug}-dropdown: {e}"
        print(msg, file=sys.stderr)
        ERRORS.append(msg)

async def capture_mobile_nav(page):
    """Mobile: open hamburger and capture open state."""
    try:
        await page.goto(BASE + "/", wait_until="networkidle", timeout=30000)
        await asyncio.sleep(1.0)
        hamburger = page.locator("button[aria-label*='menu'], button.hamburger, .nav-toggle, [class*='hamburger']").first
        await hamburger.click()
        await asyncio.sleep(0.6)
        fname = OUT / "home-mobile-nav-open.png"
        await page.screenshot(path=str(fname), full_page=False)
        print(f"OK  {fname.name}")
    except Exception as e:
        msg = f"FAIL mobile-nav-open: {e}"
        print(msg, file=sys.stderr)
        ERRORS.append(msg)

async def main():
    async with async_playwright() as p:
        browser = await p.firefox.launch(headless=True)

        for vp_name, vp in VIEWPORTS:
            ctx = await browser.new_context(viewport=vp)
            page = await ctx.new_page()

            for slug, route in ROUTES:
                await capture(page, BASE + route, slug, vp_name)

            if vp_name == "desktop":
                await capture_dropdown(page, "services-nav")

            if vp_name == "mobile":
                await capture_mobile_nav(page)

            await ctx.close()

        await browser.close()

    if ERRORS:
        err_file = OUT / "errors.txt"
        err_file.write_text("\n".join(ERRORS))
        print(f"\n{len(ERRORS)} error(s) -- see {err_file}")
    print(f"\nDone. Screenshots in {OUT}")

asyncio.run(main())
