import json,urllib.request,csv
from pathlib import Path
from collections import Counter
H=Path(__file__).resolve().parent
UA={"User-Agent":"incoprea-research/1.0"}
try:
    u="https://api.fda.gov/drug/shortages.json?limit=1000"
    d=json.loads(urllib.request.urlopen(urllib.request.Request(u,headers=UA),timeout=90).read())
    res=d.get("results",[])
    keys=sorted({k for r in res for k in r.keys()})
    (H/"raw.json").write_text(json.dumps({"n":len(res),"keys":keys,"results":res[:50]},indent=2))
    rows=[]
    for r in res:
        rows.append({k:(str(v)[:200] if not isinstance(v,(int,float)) else v) for k,v in r.items()})
    if rows:
        with (H/"shortages.csv").open("w",newline="") as f:
            w=csv.DictWriter(f,fieldnames=list(rows[0])); w.writeheader(); w.writerows(rows)
    L=["# FDA drug shortages — data reconnaissance","",
    f"Endpoint returned **{len(res)}** records. Available fields: {', '.join(keys[:15])}.","",
    "This run establishes whether the shortage feed can support a supply-chain analysis. It does not",
    "yet answer the question; the fields and completeness need review first.",""]
    (H/"FINDINGS.md").write_text("\n".join(L))
    print("shortages records",len(res),"keys",keys[:10])
except Exception as e:
    (H/"FINDINGS.md").write_text(f"# FDA drug shortages\n\nEndpoint failed: {e}\n\nNo data retrieved; recorded as a failure rather than filled in.\n")
    print("shortages FAILED:",e)
