#!/usr/bin/env python3
"""Tangkap gambar tombol WhatsApp di halaman /jasa-seo-surabaya/ supaya bisa dinilai visualnya."""
from playwright.sync_api import sync_playwright

URL = "https://studiosoft.id/jasa-seo-surabaya/"
with sync_playwright() as p:
    b = p.chromium.launch(args=["--no-sandbox", "--disable-dev-shm-usage"])
    hal = b.new_page(viewport={"width": 420, "height": 900},
                     user_agent="Mozilla/5.0 (iPhone; CPU iPhone OS 17_0 like Mac OS X) AppleWebKit/605.1.15 "
                                "(KHTML, like Gecko) Version/17.0 Mobile/15E148 Safari/604.1")
    hal.goto(URL, wait_until="domcontentloaded", timeout=90000)
    hal.wait_for_timeout(4000)
    tombol = hal.locator('a[href*="wa.me/6281935000624"]').all()
    print(f"tombol WA di halaman: {len(tombol)}")
    for i, t in enumerate(tombol):
        try:
            teks = (t.inner_text() or "").strip()[:40]
            tampak = t.is_visible()
            kotak = t.bounding_box()
            print(f"   {i+1}. «{teks}» · tampak={tampak} · ukuran={kotak['width']:.0f}x{kotak['height']:.0f}"
                  if kotak else f"   {i+1}. «{teks}» · tampak={tampak} · tidak berukuran")
        except Exception as e:
            print(f"   {i+1}. gagal dibaca: {str(e)[:40]}")
    # tangkap bagian tombol isi (yang di dalam artikel)
    for i, t in enumerate(tombol):
        kotak = t.bounding_box()
        if kotak and kotak["height"] > 20:
            t.scroll_into_view_if_needed()
            hal.wait_for_timeout(800)
            hal.screenshot(path=f"/tmp/tombol_{i+1}.png", clip={
                "x": 0, "y": max(0, kotak["y"] - 60), "width": 420,
                "height": min(260, kotak["height"] + 120)})
            print(f"   tangkapan → /tmp/tombol_{i+1}.png")
            break
    b.close()
