#!/usr/bin/env python3
"""Periksa apakah widget ulasan Trustindex benar-benar tampil, dan apakah asetnya termuat."""
import urllib.request, re
from playwright.sync_api import sync_playwright

URL = "https://studiosoft.id/"
UA = {"User-Agent": "Mozilla/5.0"}

print("=== aset widget Trustindex ===")
h = urllib.request.urlopen(urllib.request.Request(URL, headers=UA), timeout=90).read().decode("utf-8", "replace")
aset = set(re.findall(r'(?:src|href)="(//studiosoft\.id/[^"]*trustindex[^"]*|[^"]*trustindex[^"]*)"', h))
for a in sorted(aset):
    u = "https:" + a if a.startswith("//") else a
    try:
        r = urllib.request.urlopen(urllib.request.Request(u, headers=UA), timeout=40)
        print(f"   HTTP {r.status} | {len(r.read())//1024:>4} KB | {u[:100]}")
    except Exception as e:
        print(f"   GAGAL {str(e)[:40]} | {u[:100]}")

with sync_playwright() as p:
    b = p.chromium.launch(args=["--no-sandbox", "--disable-dev-shm-usage"])
    for nama, w, mob in (("PONSEL 390", 390, True), ("DESKTOP 1280", 1280, False)):
        ctx = b.new_context(viewport={"width": w, "height": 900}, is_mobile=mob, has_touch=mob, device_scale_factor=1)
        pg = ctx.new_page()
        pg.goto(URL, wait_until="load", timeout=90000)
        pg.wait_for_timeout(7000)
        d = pg.evaluate("""() => {
          const w = document.querySelector('.ti-widget');
          if (!w) return {ada:false};
          const s = getComputedStyle(w); const r = w.getBoundingClientRect();
          const p = w.closest('pre');
          return {ada:true, opacity:s.opacity, display:s.display, visibility:s.visibility,
                  tinggi:Math.round(r.height), lebar:Math.round(r.width),
                  gayaInline:w.getAttribute('style')||'',
                  preAda:!!p, preGaya: p ? (p.getAttribute('style')||'(tanpa style)') : '-',
                  judulWidget:(w.querySelector('.ti-header, .ti-rating-text, h2, .ti-widget-header')||{}).textContent||'',
                  teksAwal:(w.textContent||'').trim().slice(0,120)};
        }""")
        print(f"\n=== {nama} ===")
        for k, v in d.items():
            print(f"   {k:<14}: {v}")
        # screenshot area widget
        try:
            el = pg.query_selector("section#block-17")
            if el:
                el.scroll_into_view_if_needed()
                pg.wait_for_timeout(1500)
                el.screenshot(path=f"ss_widget_footer_{w}.png")
                print(f"   screenshot area widget: ss_widget_footer_{w}.png")
        except Exception as e:
            print("   screenshot gagal:", str(e)[:60])
        ctx.close()
    b.close()
