"""Does `frames` actually cover every company? Empirical test on the Mag 7."""
import urllib.request, json
H={"User-Agent":"Your Name your@email.com"}
def get(u):
    with urllib.request.urlopen(urllib.request.Request(u,headers=H),timeout=40) as r:
        return json.loads(r.read())

CIK={"Apple":320193,"Microsoft":789019,"Alphabet":1652044,"Amazon":1018724,
     "Nvidia":1045810,"Meta":1326801,"Tesla":1318605}
TAG="NetCashProvidedByUsedInOperatingActivities"

print("FRAMES coverage test: us-gaap/%s/USD\n" % TAG)
for frame in ["CY2025Q1","CY2025Q2","CY2025Q3","CY2025Q4","CY2026Q1"]:
    try:
        j=get(f"https://data.sec.gov/api/xbrl/frames/us-gaap/{TAG}/USD/{frame}.json")
    except Exception as e:
        print(f"{frame}: FAILED {e}"); continue
    got={d["cik"]:d for d in j["data"]}
    present=[n for n,c in CIK.items() if c in got]
    missing=[n for n,c in CIK.items() if c not in got]
    print(f"{frame}  total filers={len(j['data']):>5}   Mag7 present={len(present)}/7   missing: {', '.join(missing) if missing else 'none'}")

print("\nDetail for CY2025Q1 (values in $mm, and the period each filer actually reported):")
j=get(f"https://data.sec.gov/api/xbrl/frames/us-gaap/{TAG}/USD/CY2025Q1.json")
got={d["cik"]:d for d in j["data"]}
for n,c in CIK.items():
    d=got.get(c)
    if d: print(f"  {n:<10} {d['val']/1e6:>10,.0f}   start={d.get('start')} end={d.get('end')}")
    else: print(f"  {n:<10} {'ABSENT':>10}")

print("\nAnd the same quarter via companyconcept, for contrast:")
for n,c in CIK.items():
    j=get(f"https://data.sec.gov/api/xbrl/companyconcept/CIK{c:010d}/us-gaap/{TAG}.json")
    f=[x for x in j["units"]["USD"] if x["end"]>="2025-03-01" and x["end"]<="2025-05-31"]
    if f: print(f"  {n:<10} {f[0]['val']/1e6:>10,.0f}   start={f[0]['start']} end={f[0]['end']}  ({f[0]['fp']} {f[0]['form']})")
    else: print(f"  {n:<10} {'no fact in window':>10}")
