#!/usr/bin/env python3
"""TSLaw: verifikasi setelah perubahan + potret tampilan beranda (desktop & ponsel) untuk cek design."""
import html, json, re, urllib.request
from playwright.sync_api import sync_playwright

UA = {"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 Chrome/124 Safari/537.36"}

print("=== 1. BERANDA (live) ===")
h = urllib.request.urlopen(urllib.request.Request("https://www.tslaw.id/", headers=UA), timeout=120).read().decode("utf-8", "replace")
t = re.search(r"<title>(.*?)</title>", h, re.S)
d = re.search(r'<meta name="description" content="([^"]*)"', h)
h1 = re.findall(r"<h1[^>]*>(.*?)</h1>", h, re.S)
print("   judul :", t.group(1) if t else "-")
print(f"   meta  : ({len(d.group(1)) if d else 0} char) {d.group(1)[:110] if d else '-'}")
print("   H1    :", [html.unescape(re.sub(r'<[^>]+>', '', x)).strip()[:70] for x in h1])
print("   H2    :", [html.unescape(re.sub(r'<[^>]+>', '', x)).strip()[:60] for x in re.findall(r"<h2[^>]*>(.*?)</h2>", h, re.S)][:6])
print("   schema Attorney/LegalService:", "ADA" if ('"Attorney"' in h or '"LegalService"' in h) else "TIDAK ADA")
print("   jumlah kata:", len([w for w in re.split(r"\s+", re.sub(r"<[^>]+>", " ", h)) if w]))
print("   ukuran HTML:", len(h) // 1024, "KB | <img>:", len(re.findall(r"<img", h)), "| script:", len(re.findall(r"<script", h)))

print("\n=== 2. /hubungi/ (live) ===")
h2 = urllib.request.urlopen(urllib.request.Request("https://www.tslaw.id/hubungi/", headers=UA), timeout=120).read().decode("utf-8", "replace")
print("   lorem ipsum masih ada :", "YA" if "Vestibulum" in h2 else "sudah tidak ada")
print("   nomor dummy +1 masih ada:", "YA" if "+1 1184" in h2 or "+1 4487" in h2 else "sudah tidak ada")
nomor = sorted(set(re.findall(r"\+62[\d\s]{8,16}", h2)))
print("   nomor asli tampil     :", nomor)
print("   email tampil          :", sorted(set(re.findall(r"[\w.+-]+@[\w-]+\.[\w.]+", re.sub(r"<[^>]+>", " ", h2))))[:3])

print("\n=== 3. potret tampilan ===")
with sync_playwright() as p:
    b = p.chromium.launch(args=["--no-sandbox", "--disable-dev-shm-usage"])
    for nama, w, mob, tinggi in (("desktop", 1280, False, 2600), ("ponsel", 390, True, 2400)):
        ctx = b.new_context(viewport={"width": w, "height": tinggi}, is_mobile=mob, has_touch=mob,
                            device_scale_factor=1)
        pg = ctx.new_page()
        err = []
        pg.on("pageerror", lambda e: err.append(str(e)[:100]))
        pg.goto("https://www.tslaw.id/", wait_until="load", timeout=120000)
        pg.wait_for_timeout(6000)
        pg.screenshot(path=f"tslaw_home_{nama}.png", full_page=False)
        lebar = pg.evaluate("() => ({s: document.documentElement.scrollWidth, i: window.innerWidth})")
        print(f"   {nama}: scrollWidth {lebar['s']} vs layar {lebar['i']} → {'meluber' if lebar['s'] > lebar['i']+2 else 'pas'}")
        print(f"      error JS di halaman: {err[:2] if err else 'tidak ada'}")
        ctx.close()
    b.close()
print("\nscreenshot: tslaw_home_desktop.png, tslaw_home_ponsel.png")
