"""Download the SEC's pre-rendered per-statement R-pages to show what they are."""
from edgar import *
set_identity("Your Name your@email.com")
from pathlib import Path
import xml.etree.ElementTree as ET, urllib.request, re

H={"User-Agent":"Your Name your@email.com"}
def fetch(u):
    with urllib.request.urlopen(urllib.request.Request(u,headers=H),timeout=40) as r: return r.read()

for tk,label in [("AAPL","Apple"),("AMZN","Amazon")]:
    f = Company(tk).get_filings(form="10-Q").latest(1)
    d = Path("filings")/f"{tk}_10Q_{f.filing_date}"/"R_pages"; d.mkdir(parents=True, exist_ok=True)
    accn = f.accession_no.replace("-","")
    base = f"https://www.sec.gov/Archives/edgar/data/{int(f.cik)}/{accn}"

    summ = fetch(f"{base}/FilingSummary.xml")
    (d/"FilingSummary.xml").write_bytes(summ)
    root = ET.fromstring(summ)
    print(f"\n=== {label} {f.form} {f.filing_date} : R-page index ===")
    wanted=[]
    for rep in root.findall(".//Report"):
        fn = rep.findtext("HtmlFileName") or rep.findtext("XmlFileName") or ""
        nm = rep.findtext("ShortName") or ""
        cat= rep.findtext("MenuCategory") or ""
        if fn.startswith("R"):
            n=int(re.sub(r"\D","",fn) or 0)
            if cat.lower()=="statements" or n<=10:
                wanted.append((fn,nm,cat)); print(f"  {fn:<8} [{cat:<12}] {nm[:70]}")
    for fn,nm,cat in wanted:
        try:
            b=fetch(f"{base}/{fn}")
            safe=re.sub(r"[^A-Za-z0-9]+","_",nm)[:48].strip("_")
            (d/f"{fn.replace('.htm','')}_{safe}.html").write_bytes(b)
        except Exception as e: print(f"    skip {fn}: {e}")
    print(f"  -> saved {len(list(d.glob('*.html')))} R-pages to {d}")
