#!/usr/bin/env python3
"""Reproduce a source-class/date-field audit from one exact public directory.
Usage: python3 reproduce-minnesota-directory-source-profile-2026-10-29.py FIXED_JSON
No network, writes, installs or inferred source-independence classifications.
"""
from pathlib import Path
from collections import Counter
import argparse,csv,hashlib,json,sys
EXPECTED='668b4bfbe392047a59621c573dda9bb75f1b5025a003fd5c94986840fad49e88'
URL='https://joshliljenquist.com/data/minnesota-editions/reference-2026-10-29-reliability/minnesota-creators-2026.json'
def rows(records):
 n=len(records);out=[]
 def add(dim,value,count,limit):out.append({'dimension':dim,'value':value,'record_count':count,'denominator':n,'unit':'directory entities','limitation':limit,'fixed_source_url':URL,'source_sha256':EXPECTED})
 for label,count in sorted(Counter(r['geography_source_class']for r in records).items()):add('recorded_primary_geography_source_class',label,count,'One primary recorded class per entity; supporting sources excluded. A publisher-hosted profile is not automatically independent reporting.')
 dated=sum(bool(r.get('geography_source_date'))for r in records)
 for value,count in [('publication_date_recorded',dated),('publication_date_not_recorded',n-dated)]:add('source_publication_date_field',value,count,'Describes stored date-field completeness, not whether a source has a publication date, remains current, or proves residence. Review and observation dates are separate.')
 return out
if __name__=='__main__':
 p=argparse.ArgumentParser();p.add_argument('input',type=Path);a=p.parse_args()
 try:
  raw=a.input.read_bytes()
  if hashlib.sha256(raw).hexdigest()!=EXPECTED:raise ValueError('Wrong or altered fixed edition')
  result=rows(json.loads(raw)['records']);w=csv.DictWriter(sys.stdout,fieldnames=list(result[0]),lineterminator='\n');w.writeheader();w.writerows(result)
 except(OSError,ValueError,KeyError)as e:raise SystemExit('Cannot reproduce: '+str(e))
