import sys, re, requests
sys.path.insert(0, "proxy")
from bs4 import BeautifulSoup
from psionproxy import profiles
from psionproxy.extract import extract

UA = ("Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 "
      "(KHTML, like Gecko) Chrome/140.0 Safari/537.36")
URLS = {"Wikipedia": "https://en.wikipedia.org/wiki/Psion_Series_7",
        "BBC News":  "https://www.bbc.co.uk/news",
        "Hacker News": "https://news.ycombinator.com/"}

for name, url in URLS.items():
    html = requests.get(url, headers={"User-Agent": UA}, timeout=25).text
    body, _ = extract(html, url, profile=profiles.CE)
    total = len(body.encode("utf-8", "replace"))
    s = BeautifulSoup(body, "html.parser")

    cls_bytes = sum(len(' class="%s"' % " ".join(t["class"]))
                    for t in s.select("[class]") if t.get("class"))
    id_bytes  = sum(len(' id="%s"' % t["id"]) for t in s.select("[id]") if t.get("id"))
    sty_attr  = sum(len(' style="%s"' % t["style"]) for t in s.select("[style]") if t.get("style"))
    sty_block = sum(len(t.get_text()) for t in s.find_all("style"))
    link_tags = s.find_all("link")

    # which class names does the retained inline CSS actually reference?
    css = " ".join(t.get_text() for t in s.find_all("style"))
    referenced = set(re.findall(r"\.([A-Za-z_][\w-]*)", css))
    used = set()
    for t in s.select("[class]"):
        for c in (t.get("class") or []):
            if c in referenced: used.add(c)
    present = {c for t in s.select("[class]") for c in (t.get("class") or [])}

    # what would external CSS cost at 19200 baud (1.9 KB/s)?
    ext = 0
    for l in link_tags:
        if "stylesheet" in " ".join(l.get("rel") or []).lower() and l.get("href"):
            try:
                h = l["href"]
                if h.startswith("//"): h = "https:" + h
                elif h.startswith("/"): h = "https://" + url.split("/")[2] + h
                ext += len(requests.get(h, headers={"User-Agent": UA}, timeout=15).content)
            except Exception:
                pass

    print(f"=== {name}  CE output {total:,}B (budget 32,000) ===")
    print(f"    class= attrs      {cls_bytes:>7,}B  ({cls_bytes*100//total}% of page)")
    print(f"    id= attrs         {id_bytes:>7,}B")
    print(f"    style= attrs      {sty_attr:>7,}B   <- self-contained, proven to render")
    print(f"    <style> blocks    {sty_block:>7,}B")
    print(f"    distinct classes  {len(present):>7}  of which referenced by retained CSS: {len(used)}")
    print(f"    external CSS      {ext:>7,}B  = {ext/1900:>5.0f}s at 19200 baud")
    print()
