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

Layers: 1 = current registered locations (keyed by SITE_APN), 23 = inspections.
Output: data/raw/rental_registry.json, data/raw/rental_inspections.json
"""
from common import arcgis_query, epoch_to_date, save

U = "https://services1.arcgis.com/mg5xPlZs10O6tWPP/arcgis/rest/services/Plan_RentalRegistrations/FeatureServer"

if __name__ == "__main__":
    reg = arcgis_query(f"{U}/1", "1=1", geom=True, page=2000)
    save("rental_registry.json", reg)
    print(len(reg), "registered locations")
    ins = arcgis_query(f"{U}/23", "1=1", "ACTIVITYID,COMPLETED_DATE,RESULT,SITE_APN", page=2000)
    for r in ins:
        r["COMPLETED_DATE"] = epoch_to_date(r.get("COMPLETED_DATE"))
    save("rental_inspections.json", ins)
    print(len(ins), "inspections")
