#!/usr/bin/env python3
"""Diagnosa responsivitas studiosoft.id: meta viewport, elemen berlebar tetap, dan CSS yang bocor ke kanan."""
import re, urllib.request

S = "https://studiosoft.id/"
UA_MOBILE = ("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")

h = urllib.request.urlopen(urllib.request.Request(S, headers={"User-Agent": UA_MOBILE}), timeout=90).read().decode("utf-8", "replace")
print(f"panjang HTML: {len(h)} karakter\n")

print("=== 1. meta viewport (INi penentu utama) ===")
vp = re.findall(r'<meta[^>]+name=["\']viewport["\'][^>]*>', h, re.I)
if vp:
    for v in vp:
        print("  ", v)
    isi = " ".join(vp)
    print("   → width=device-width :", "YA" if "device-width" in isi else "TIDAK")
    print("   → user-scalable      :", "dibatasi" if re.search(r"user-scalable\s*=\s*no|maximum-scale\s*=\s*1", isi) else "boleh")
else:
    print("   ❌ TIDAK ADA meta viewport di HTML (ini penyebab klasik halaman tampil ala desktop)")

print("\n=== 2. tema & page builder yang dipakai ===")
tema = set(re.findall(r"/wp-content/themes/([a-z0-9\-_]+)/", h, re.I))
print("   tema:", sorted(tema))
plug = sorted(set(re.findall(r"/wp-content/plugins/([a-z0-9\-_]+)/", h, re.I)))
print("   plugin aktif:", plug[:18])

print("\n=== 3. elemen dengan lebar tetap besar (biang overflow) ===")
pola = re.findall(r'style="[^"]*(?:^|;|\s)(?:min-)?width\s*:\s*(\d{3,4})px', h, re.I)
besar = sorted({int(x) for x in pola if int(x) > 420}, reverse=True)
print("   lebar tetap >420px di inline style:", besar[:10] if besar else "tidak ada")
for m in re.finditer(r'<[a-z]+[^>]+style="[^"]*(?:min-)?width\s*:\s*(?:[6-9]\d{2}|[1-9]\d{3})px[^"]*"[^>]*>', h, re.I):
    print("   ", re.sub(r"\s+", " ", m.group(0))[:150])

print("\n=== 4. tag <img>/<iframe> tanpa pembatas lebar ===")
img_tanpa = [m.group(0)[:110] for m in re.finditer(r"<img[^>]+>", h, re.I)
             if not re.search(r"max-width|width=\"100%\"|style=\"[^\"]*max-width", m.group(0), re.I)]
print(f"   {len(img_tanpa)} gambar tanpa max-width 100% (contoh 3):")
for t in img_tanpa[:3]:
    print("   ", t)
iframe = re.findall(r"<iframe[^>]+>", h, re.I)
print(f"   {len(iframe)} iframe:")
for f in iframe[:3]:
    print("   ", re.sub(r"\s+", " ", f)[:130])

print("\n=== 5. tabel / elemen dikenal suka melebar ===")
for tag in ("table", "pre", "code"):
    n = len(re.findall(rf"<{tag}[\s>]", h, re.I))
    if n:
        print(f"   <{tag}>: {n}")

print("\n=== 6. cek CSS eksternal: media query mobile ===")
css = re.findall(r'<link[^>]+rel=["\']stylesheet["\'][^>]+href=["\']([^"\']+\.css[^"\']*)["\']', h, re.I)
css = [c if c.startswith("http") else "https://studiosoft.id" + c for c in css]
print(f"   {len(css)} file CSS; memeriksa {min(len(css), 6)} pertama")
for u in css[:6]:
    try:
        t = urllib.request.urlopen(urllib.request.Request(u, headers={"User-Agent": UA_MOBILE}), timeout=40).read().decode("utf-8", "replace")
        mq = len(re.findall(r"@media", t))
        maxw = len(re.findall(r"@media[^{]*max-width", t))
        print(f"     {'/'.join(u.split('/')[-2:])[:52]:<54} {len(t)//1024:>5} KB | @media {mq:>4} | max-width {maxw:>4}")
    except Exception as e:
        print(f"     {u.split('/')[-1][:40]:<42} gagal: {str(e)[:40]}")
