#!/usr/bin/env python3
import asyncio, json
from playwright.async_api import async_playwright

BASE = "http://localhost:8011"
OUT = "/home/sirdrez/arisingmedia-websites/bigbreath.co/.planning/qa/visual-2026-06-01-post-fix"
PAGES = [
    ("home", "/"),
    ("about", "/about"),
    ("services", "/services"),
    ("testimonials", "/testimonials"),
    ("gallery", "/gallery"),
]

async def scroll_and_screenshot(page, url, path):
    await page.goto(url, wait_until="networkidle")
    # scroll top to bottom in 200px steps with 100ms pause
    scroll_height = await page.evaluate("document.body.scrollHeight")
    pos = 0
    while pos < scroll_height:
        await page.evaluate(f"window.scrollTo(0, {pos})")
        await asyncio.sleep(0.1)
        pos += 200
    await page.evaluate("window.scrollTo(0, 0)")
    await asyncio.sleep(2)
    await page.screenshot(path=path, full_page=True)

async def count_elements(page, url):
    await page.goto(url, wait_until="networkidle")
    counts = await page.evaluate("""() => ({
        faq:          document.querySelectorAll('[class*="faq"]').length,
        stat:         document.querySelectorAll('[class*="stat"]').length,
        footprint:    document.querySelectorAll('[class*="footprint"]').length,
        reveal:       document.querySelectorAll('.bb-reveal').length,
        section:      document.querySelectorAll('.bb-section').length,
        images:       document.querySelectorAll('img').length,
        reveal_opacity: (() => {
            const el = document.querySelector('.bb-reveal');
            return el ? getComputedStyle(el).opacity : 'N/A';
        })(),
    })""")
    return counts

async def main():
    async with async_playwright() as pw:
        browser = await pw.firefox.launch()
        ctx = await browser.new_context(viewport={"width": 1440, "height": 900})

        results = {}
        for name, path in PAGES:
            page = await ctx.new_page()
            url = BASE + path
            img_path = f"{OUT}/{name}.png"
            await scroll_and_screenshot(page, url, img_path)
            counts = await count_elements(page, url)
            results[name] = counts
            print(f"SCREENSHOT: {img_path}")
            print(f"COUNTS {name}: {json.dumps(counts)}")
            await page.close()

        await browser.close()

asyncio.run(main())
