"""Reproduce descriptive field coverage of two exact public exports. No network or recoding."""
import csv
import hashlib
import json
from pathlib import Path
import sys

HOOK_SHA = 'd854e2fec64db8a8ae1170a21bde3fcea58b1f73f8ffc6eafdca9ce83dc3345c'
STORY_SHA = 'd386334259a103c84a350081825bc9f2c774553b3fda1295c29aa992caa4b3d1'

def coverage(hook_bytes, story_bytes):
    if hashlib.sha256(hook_bytes).hexdigest() != HOOK_SHA or hashlib.sha256(story_bytes).hexdigest() != STORY_SHA:
        raise ValueError('Use the exact original exports; check both SHA256 digests. Corrected views are separate.')
    hooks, story = json.loads(hook_bytes), json.loads(story_bytes)
    a, b = hooks['records'], story['records']
    if len(a) != 52 or len(b) != 52 or story['sourceCorpusSha256'] != HOOK_SHA:
        raise ValueError('Unexpected source relationship or denominator')
    by_id = {r['researchId']: r for r in a}
    if len(by_id) != 52 or len({r['researchId'] for r in b}) != 52:
        raise ValueError('Ambiguous record identity')
    if any(r['researchId'] not in by_id or r['sourceVideoId'] != by_id[r['researchId']]['sourceVideoId'] for r in b):
        raise ValueError('Unmatched source edition')
    entries = [
        ('matched-editions', 'Matching source video editions', 52, 'Joined by researchId and sourceVideoId; not 104 separate videos.'),
        ('detailed-breakdowns', 'Detailed public story breakdowns', sum(bool(r['detailedPublicExample']) for r in b), 'A subset of the 52 editions, not additional observations.'),
        ('visual-description-stored', 'Opening visual description stored', sum(r.get('openingVisualDescription') not in [None, ''] for r in a), 'Stored description field; not a comprehensive visual analysis.'),
        ('visual-description-unknown', 'Opening visual description missing', sum(r.get('openingVisualDescription') in [None, ''] for r in a), 'Unknown or not separately coded; never converted to a zero-valued visual feature.'),
        ('view-snapshot-stored', 'Public view-count snapshot stored', sum(r.get('publicViewCountSnapshot') is not None for r in a), 'Missing snapshot is not zero views; performance cannot be compared from this field.'),
        ('full-story-lead-coded', 'Full-story speaker lead coded', sum(r.get('fullStoryLead') not in [None, ''] for r in b), 'Opening lead is a separate field; it cannot establish full-video speaker share.')
    ]
    return [dict(metric_id=key, metric=label, editions=count, denominator_editions=52, scope=note, hook_source_sha256=HOOK_SHA, story_source_sha256=STORY_SHA) for key,label,count,note in entries]

def write(rows, out):
    writer = csv.DictWriter(out, fieldnames=list(rows[0]), lineterminator='\n')
    writer.writeheader()
    writer.writerows(rows)

if __name__ == '__main__':
    if len(sys.argv) != 3:
        raise SystemExit('Usage: python3 reproduce-short-form-evidence-coverage-2026.py short-form-hook-corpus-2026.json storytelling-corpus-2026.json')
    try:
        write(coverage(Path(sys.argv[1]).read_bytes(), Path(sys.argv[2]).read_bytes()), sys.stdout)
    except (ValueError, KeyError) as error:
        raise SystemExit(str(error))
