"""Reproduce the published hook/payoff matrix from the exact original public corpus.

Usage: python3 reproduce-hook-payoff-matrix-2026.py short-form-hook-corpus-2026.json
Writes CSV to stdout. Counts annotated video editions, not creators or independently verified outcomes.
"""
import csv,hashlib,json,sys
from collections import Counter
EXPECTED_SHA256 = "d854e2fec64db8a8ae1170a21bde3fcea58b1f73f8ffc6eafdca9ce83dc3345c"
def matrix(raw):
 if hashlib.sha256(raw).hexdigest()!=EXPECTED_SHA256:raise ValueError('This matrix requires the exact published version1 source; check its SHA256.')
 d=json.loads(raw);records=d['records'];hooks=d['stats']['hookTypeDistribution'];payoffs=d['stats']['payoffTypeDistribution'];n=len(records)
 if n!=52 or len({r['researchId']for r in records})!=n:raise ValueError('Unexpected edition identities')
 counts=Counter((r['primaryHookType'],r['payoffType'])for r in records);labels_h={r['primaryHookType']:r['primaryHookLabel']for r in records};labels_p={r['payoffType']:r['payoffLabel']for r in records}
 if {h:sum(counts[h,p]for p in payoffs)for h in hooks}!=hooks or {p:sum(counts[h,p]for h in hooks)for p in payoffs}!=payoffs:raise ValueError('Original category marginals do not reconcile')
 return [dict(hook_id=h,hook_label=labels_h[h],payoff_id=p,payoff_label=labels_p[p],video_edition_count=counts[h,p],denominator_video_editions=n,source_dataset_id=d['datasetId'],source_version=d['version'],source_sha256=EXPECTED_SHA256)for h in hooks for p in payoffs]
def main():
 if len(sys.argv)!=2:raise SystemExit(__doc__)
 with open(sys.argv[1],'rb')as f:rows=matrix(f.read())
 writer=csv.DictWriter(sys.stdout,fieldnames=list(rows[0]),lineterminator='\n');writer.writeheader();writer.writerows(rows)
if __name__=='__main__':main()
