import json, urllib.request, urllib.parse, csv, datetime
B="https://maps.cob.org/arcgis3/rest/services/Maps/Permits/MapServer/0/query"
FIELDS="PERMIT_NO,PERMIT_CATEGORY,PERMIT_CLASS,PermitType,PermitSubType,STATUS,APPLIED,APPROVED,ISSUED,FINALED,EXPIRED,PARENT_PROJECT_NO,PARENT_PERMIT_NO,SITE_ADDR,DESCRIPTION,SCOPEOFWORK,JOBVALUE,Permit_Value,OWNER_NAME,APPLICANT_NAME,CONTRACTOR_NAME,NEW_FLOOR_AREA,TOTAL_FLOOR_AREA,NUM_STORIES,NEW_DWELL_UNITS,NUM_NEW_UNITS,EXIST_DWELL_UNITS,Neighborhood,Neighborhood_Subarea,Zoning_Description,Housing_Type,Num_Studios,Num_1Beds,Num_2Beds,Num_3Beds,Num_4Beds,SITE_APN"
where="APPLIED >= DATE '2025-01-01' AND PERMIT_CATEGORY = 'BUILDING'"
rows=[]; off=0
while True:
    q=urllib.parse.urlencode({"where":where,"outFields":FIELDS,"returnGeometry":"true","outSR":"4326","f":"json","resultOffset":off,"resultRecordCount":2000,"orderByFields":"APPLIED DESC"})
    d=json.load(urllib.request.urlopen(B+"?"+q))
    feats=d.get("features",[])
    for f in feats:
        a=f["attributes"]; g=f.get("geometry") or {}
        a["lon"]=g.get("x"); a["lat"]=g.get("y")
        for k in ("APPLIED","APPROVED","ISSUED","FINALED","EXPIRED"):
            if a.get(k): a[k]=datetime.datetime.utcfromtimestamp(a[k]/1000).date().isoformat()
        rows.append(a)
    if not d.get("exceededTransferLimit"): break
    off+=len(feats)
print("rows",len(rows))
json.dump(rows,open("building_2025plus.json","w"))
with open("building_2025plus.csv","w",newline="") as fh:
    w=csv.DictWriter(fh,fieldnames=list(rows[0].keys())); w.writeheader(); w.writerows(rows)
from collections import Counter
print("\nHousing_Type:",Counter(r["Housing_Type"] for r in rows).most_common())
print("\nNUM_NEW_UNITS:",Counter(r["NUM_NEW_UNITS"] for r in rows).most_common(15))
print("\nNEW_DWELL_UNITS:",Counter(r["NEW_DWELL_UNITS"] for r in rows).most_common(15))
print("\nPermitType:",Counter(r["PermitType"] for r in rows).most_common())
print("\nPermitSubType for NEW CONSTRUCTION types:",Counter((r["PermitType"],r["PermitSubType"]) for r in rows if "NEW CONSTRUCTION" in (r["PermitType"] or "") or "MULTIFAMILY" in (r["PermitType"] or "")).most_common(30))
