#!/usr/bin/env python3
"""Python 3 standard-library audit of the published output, not a GTFS/network rerun.
Run beside tracts.geojson: python3 audit-data.py
"""
import csv
import hashlib
import json
import math
from pathlib import Path

BASE = Path(__file__).resolve().parent
RAW = (BASE / 'tracts.geojson').read_bytes()
ROWS = [f['properties'] for f in json.loads(RAW)['features']]
SHA = hashlib.sha256(RAW).hexdigest()
EXPECTED = 'bc818cfd8fc50c43b3927713dc59da1062f2b8d3947f8a25c75f660544b3006c'
CHECKS = []


def check(name, condition, detail):
    CHECKS.append(dict(check=name, passed=bool(condition), detail=detail))


def weighted(rows, key):
    v = [r for r in rows if r.get(key) is not None and r['pop_2024'] > 0]
    denominator = sum(r['pop_2024'] for r in v)
    return sum(r[key] * r['pop_2024'] for r in v) / denominator if denominator else None


def summarize(rows, name):
    v = [r for r in rows if r['accessible_weekday_departures'] is not None and r['accessible_saturday_departures'] is not None]
    w, s = weighted(v, 'accessible_weekday_departures'), weighted(v, 'accessible_saturday_departures')
    lower = [r for r in v if r['accessible_saturday_departures'] < r['accessible_weekday_departures']]
    lost = [r for r in v if r['accessible_weekday_departures'] > 0 and r['accessible_saturday_departures'] == 0]
    return dict(area=name, tracts=len(rows), tracts_with_paired_service=len(v), tracts_missing_service=len(rows)-len(v), resident_population_denominator=sum(r['pop_2024'] for r in v), tracts_positive_weekday=sum(r['accessible_weekday_departures'] > 0 for r in v), tracts_positive_saturday=sum(r['accessible_saturday_departures'] > 0 for r in v), tracts_lower_saturday=len(lower), tract_share_lower_saturday=len(lower)/len(v) if v else None, tracts_weekday_positive_saturday_zero=len(lost), residents_in_tracts_with_lower_saturday_average=sum(r['pop_2024'] for r in lower), residents_in_tracts_weekday_positive_saturday_zero=sum(r['pop_2024'] for r in lost), resident_weighted_weekday_departures=w, resident_weighted_saturday_departures=s, resident_weighted_departure_reduction_fraction=1-s/w if w else None, resident_share_any_fixed_route_recorded=weighted(v,'any_fixed_route_access_share'), resident_share_15_minute_coverage_recorded=weighted(v,'access_15_share'), resident_weighted_15_minute_useful_hours_recorded=weighted(v,'useful_hours_15'))


V = [r for r in ROWS if r['accessible_weekday_departures'] is not None and r['accessible_saturday_departures'] is not None]
SHARES = ['access_15_share','access_30_share','access_60_share','rail_access_share','frequent_bus_access_share','any_fixed_route_access_share','zero_car_share','transit_commute_share']
SERVICE = ['accessible_weekday_departures','accessible_saturday_departures','sensitivity_weekday_departures','useful_hours_15','useful_hours_30','useful_hours_60']
check('input_version',SHA==EXPECTED,SHA)
check('unique_tracts',len(ROWS)==len({r['GEOID'] for r in ROWS})==2175,len(ROWS))
check('counties',len({r['county'] for r in ROWS})==21,21)
check('paired_service_records',len(V)==2172,len(V))
check('finite_numeric_values',all(not isinstance(x,(int,float)) or math.isfinite(x) for r in ROWS for x in r.values()),'All numeric values finite')
check('share_bounds',all(r[k] is None or 0<=r[k]<=1 for r in ROWS for k in SHARES),'Shares within zero and one')
check('nonnegative_service',all(r[k] is None or r[k]>=0 for r in ROWS for k in SERVICE),'Recorded service nonnegative')
check('nested_frequency_coverage',all(r['access_15_share']<=r['access_30_share']+1e-10<=r['access_60_share']+2e-10 for r in V),'15-minute <= 30-minute <= 60-minute coverage')
check('nested_useful_hours',all(r['useful_hours_15']<=r['useful_hours_30']+1e-10<=r['useful_hours_60']+2e-10<=18+3e-10 for r in V),'Ordered useful hours within 18-hour window')
check('walking_sensitivity',all(r['sensitivity_weekday_departures']+1e-10>=r['accessible_weekday_departures'] for r in V),'Longer walks never reduce departures')
INDEX_BANDS = ['Low','Emerging','Supportive','Strong','Very Strong']
check('land_use_band_labels',all(INDEX_BANDS[sum(r['ts_'+case]>=b for b in [50,100,150,200])]==r['band_'+case] for r in ROWS for case in ['current','trend2050','concentrated2050']),'Current and both 2050 scenarios match revised screening bands at 50, 100, 150, 200')
error=max(abs(r['ts_current']-50*math.log2(1+r['hh_density_2024']/3)-50*math.log2(1+r['job_density_2024']/4)) for r in ROWS)
check('land_use_formula',error<1e-8,dict(maximum_absolute_error=error))
error=max(abs(r['service_gap_percentile']-r['ts_pct_current']+r['accessible_service_percentile']) for r in V)
check('rank_difference_formula',error<1e-8,dict(maximum_absolute_error=error))
SUMMARIES=[summarize(ROWS,'New Jersey')]+[summarize([r for r in ROWS if r['county']==c],c) for c in sorted({r['county'] for r in ROWS})]
state=SUMMARIES[0]
check('published_counts',[state[k] for k in ['tracts_positive_weekday','tracts_positive_saturday','tracts_lower_saturday']]==[1756,1605,1654],[1756,1605,1654])
result=dict(edition='August 2026 published web extract',audit_date='2026-11-03',input=dict(file='tracts.geojson',sha256=SHA,bytes=len(RAW)),output_arithmetic_status='passed' if all(c['passed'] for c in CHECKS) else 'failed',source_to_output_reproduction_status='not_established_for_this_extract',checks=CHECKS,statewide=state,summary_formula='sum(tract population × tract resident-weighted value) / sum(tract population), using paired non-null service records',interpretation=dict(resident_weighted_departures='Mean modeled trips reachable from a resident origin block during 6 a.m.–midnight. Not distinct statewide trips, ridership, route frequency, or destination accessibility.',residents_in_tracts='Residents live in tracts with a lower average; this does not establish each individual change.',recorded_frequency='Exact temporal qualification and route-direction treatment have not been matched to an executable run for this extract.',zeros='Per-tract network-connection and operator-completeness diagnostics are unavailable.'))
(BASE/'data-audit.json').write_text(json.dumps(result,indent=2)+'\n')
with (BASE/'county-summary.csv').open('w',newline='') as out:
    writer=csv.DictWriter(out,fieldnames=state.keys());writer.writeheader();writer.writerows(SUMMARIES)
print(json.dumps(dict(status=result['output_arithmetic_status'],checks=len(CHECKS),statewide=state),indent=2))
raise SystemExit(0 if result['output_arithmetic_status']=='passed' else 1)
