"""Exact-source descriptive category overlap. No network, recoding or population inference."""
import csv,hashlib,json,re,sys
from pathlib import Path

SOURCE_SHA='668b4bfbe392047a59621c573dda9bb75f1b5025a003fd5c94986840fad49e88'
def analyze(data):
    if hashlib.sha256(data).hexdigest()!=SOURCE_SHA:
        raise ValueError('Use the exact directory source; check its SHA256 digest.')
    records=json.loads(data)['records']
    if len(records)!=47 or len({r['entity_id']for r in records})!=47:
        raise ValueError('Unexpected directory identities or denominator')
    groups={word:{r['entity_id']for r in records if word in re.findall('[a-z]+',r['category'].lower())}for word in ['food','education','travel']}
    union=set.union(*groups.values());overlap=groups['food']&groups['travel']
    entries=[('food','Food category word','creator_records',len(groups['food']),47),('education','Education category word','creator_records',len(groups['education']),47),('travel','Travel category word','creator_records',len(groups['travel']),47),('food-and-travel','Both Food and Travel','creator_records',len(overlap),47),('any-topic','At least one of the three topic words','creator_records',len(union),47),('outside-topics','None of the three topic words','creator_records',len(records)-len(union),47),('memberships','Total topic memberships','label_assignments',sum(map(len,groups.values())),None)]
    rows=[dict(metric_id=k,metric=label,unit=unit,count=n,denominator_records=denom,source_sha256=SOURCE_SHA)for k,label,unit,n,denom in entries]
    return rows,groups
def write(rows,out):
    w=csv.DictWriter(out,fieldnames=list(rows[0]),lineterminator='\n');w.writeheader();w.writerows(rows)
if __name__=='__main__':
    if len(sys.argv)!=2:raise SystemExit('Usage: python3 reproduce-minnesota-topic-overlap-2026.py minnesota-creators-2026.json')
    try:write(analyze(Path(sys.argv[1]).read_bytes())[0],sys.stdout)
    except(ValueError,KeyError)as e:raise SystemExit(str(e))
