"""Pull Bellingham's rental registration (public ArcGIS Online layer behind the city's 'Registered Rentals' dashboard).

Dashboard: https://bellingham.maps.arcgis.com/apps/dashboards/2ffe2095a1fe41bea424e9f341539bb1  (linked from cob.org/services/permits/rentals)
Layers: 1 = current registered locations (keyed by SITE_APN), 23 = inspections, 24 = unit-count summary.
Output: feasibility/raw/rental_registry.json, feasibility/raw/rental_inspections.json
"""
import json, urllib.request, urllib.parse
U = "https://services1.arcgis.com/mg5xPlZs10O6tWPP/arcgis/rest/services/Plan_RentalRegistrations/FeatureServer"

def pull(layer, geom):
    rows, off = [], 0
    while True:
        q = urllib.parse.urlencode({"where": "1=1", "outFields": "*", "returnGeometry": str(geom).lower(), "outSR": "4326",
                                    "f": "json", "resultOffset": off, "resultRecordCount": 2000, "orderByFields": "OBJECTID"})
        d = json.load(urllib.request.urlopen(f"{U}/{layer}/query?{q}", timeout=60))
        feats = d.get("features", [])
        for f in feats:
            a = f["attributes"]; g = f.get("geometry") or {}
            if geom: a["lon"], a["lat"] = g.get("x"), g.get("y")
            rows.append(a)
        if not d.get("exceededTransferLimit") and len(feats) < 2000: return rows
        off += len(feats)

if __name__ == "__main__":
    reg = pull(1, True); json.dump(reg, open("feasibility/raw/rental_registry.json", "w")); print(len(reg), "registered locations")
    ins = pull(23, False); json.dump(ins, open("feasibility/raw/rental_inspections.json", "w")); print(len(ins), "inspections")
