#!/usr/bin/env python3
"""Screenshot bloquo.cc — homepage + portfolio pieces"""

from playwright.sync_api import sync_playwright
import time

OUTPUT_DIR = "/root/.openclaw/workspace/work/tools/visual-intake/bloquo"

def screenshot_page(page, url, filename, wait_ms=4000):
    print(f"Navigating to {url}...")
    page.goto(url, wait_until="networkidle", timeout=30000)
    time.sleep(wait_ms / 1000)
    filepath = f"{OUTPUT_DIR}/{filename}"
    page.screenshot(path=filepath, full_page=True)
    print(f"Saved: {filepath}")
    return filepath

def main():
    with sync_playwright() as p:
        browser = p.chromium.launch(headless=True, args=["--no-sandbox", "--disable-setuid-sandbox"])
        context = browser.new_context(viewport={"width": 1440, "height": 900})
        page = context.new_page()

        saved = []

        # Homepage
        saved.append(screenshot_page(page, "https://bloquo.cc/", "bloquo-homepage.png"))

        # Gather portfolio links
        page.goto("https://bloquo.cc/", wait_until="networkidle", timeout=30000)
        time.sleep(3)
        
        # Try to find portfolio/project links
        links = page.evaluate("""() => {
            const anchors = Array.from(document.querySelectorAll('a'));
            return anchors
                .map(a => ({ href: a.href, text: a.innerText.trim() }))
                .filter(l => l.href && l.href.startsWith('http') && l.href !== 'https://bloquo.cc/' && l.href !== window.location.href)
                .slice(0, 10);
        }""")
        print("Links found:", links)

        # Screenshot portfolio pieces - pick first 2-3 unique internal links
        internal = [l for l in links if 'bloquo.cc' in l['href']][:3]
        
        for i, link in enumerate(internal):
            fname = f"bloquo-project-{i+1}.png"
            try:
                saved.append(screenshot_page(page, link['href'], fname))
            except Exception as e:
                print(f"Failed {link['href']}: {e}")

        browser.close()
        print("\nAll screenshots saved:")
        for s in saved:
            print(f"  {s}")

if __name__ == "__main__":
    main()
