"""Reproduce the definition-weight sensitivity table using Python 3 only.

Usage: python3 reproduce.py [output-directory]
Starts from the included public aggregates, not private application records.
"""
import csv
import json
import sys
from pathlib import Path

ROOT = Path(__file__).resolve().parent

def calculate():
    with (ROOT / 'planning_unit_sensitivity.csv').open() as f:
        raw = list(csv.DictReader(f))
    assert len(raw) == 24
    for r in raw:
        for k in ['authorities', 'all_approval_unit_mentions',
                  'excluding_reserved_matters_unit_mentions', 'removed_unit_mentions']:
            r[k] = int(r[k])
        assert r['all_approval_unit_mentions'] == r['excluding_reserved_matters_unit_mentions'] + r['removed_unit_mentions']
    result = []
    for g in dict.fromkeys(r['geography'] for r in raw):
        data = {r['quarter']: r for r in raw if r['geography'] == g}
        c = data['2026-Q3']
        for period in ['2025-Q3', '2026-Q2']:
            b = data[period]
            assert b['authorities'] == c['authorities']
            f0, f1 = [r['excluding_reserved_matters_unit_mentions'] for r in (b,c)]
            r0, r1 = [r['removed_unit_mentions'] for r in (b,c)]
            df, dr = f1-f0, r1-r0
            crossing = -df/dr if dr else None
            interior = crossing is not None and 0 < crossing < 1
            if interior:
                assert abs(df + crossing*dr) < 1e-8
            # With a positive denominator, the sign is the affine numerator's sign.
            assert f0 > 0 and f0+r0 > 0
            result.append(dict(geography=g, comparison='Annual' if period=='2025-Q3' else 'Quarterly',
                baseline_quarter=period, current_quarter='2026-Q3', authorities=c['authorities'],
                baseline_filtered=f0, current_filtered=f1, baseline_removed=r0, current_removed=r1,
                filtered_change=df, removed_change=dr, all_change=df+dr,
                filtered_change_pct=100*df/f0, all_change_pct=100*(df+dr)/(f0+r0),
                within_range_crossing=interior, zero_change_weight_pct=100*crossing if interior else '',
                direction_at_zero='Rise' if df>0 else 'Fall' if df<0 else 'Flat',
                direction_at_one='Rise' if df+dr>0 else 'Fall' if df+dr<0 else 'Flat'))
    assert len(result)==16
    assert sum(r['within_range_crossing'] for r in result)==1
    return result

if __name__=='__main__':
    output = Path(sys.argv[1]) if len(sys.argv)>1 else ROOT
    output.mkdir(parents=True, exist_ok=True)
    rows=calculate()
    with (output/'definition_thresholds.csv').open('w', newline='') as f:
        w=csv.DictWriter(f, fieldnames=list(rows[0]));w.writeheader();w.writerows(rows)
    (output/'thresholds.json').write_text(json.dumps(rows,indent=2)+'\n')
    print('Reproduced 16 comparisons from 24 aggregate source rows; all identities passed.')
