"""Create a corrected view without modifying a frozen historical research export.

Usage: python apply-short-form-corpus-erratum-2026-10-29.py ORIGINAL.json ERRATUM.json
Prints JSON to stdout. Accepts only the two exact public inputs named in the erratum.
"""
from pathlib import Path
import hashlib, json, sys

def corrected_view(original, erratum):
    original, erratum = Path(original), Path(erratum)
    correction = json.loads(erratum.read_text())
    if correction['erratumId'] != 'JLI-CORPUS-ERRATUM-20261029-001':
        raise ValueError('Unknown correction')
    target = next((r for r in correction['inputs'] if r['filename'] == original.name), None)
    if not target or hashlib.sha256(original.read_bytes()).hexdigest() != target['sha256']:
        raise ValueError('Original export does not match the fixed edition')
    data = json.loads(original.read_text())
    matches = [r for r in data['records'] if r['sourceVideoId'] == correction['sourceVideoId']]
    if len(matches) != 1 or matches[0]['publicationDate'] != correction['before']:
        raise ValueError('Unexpected record or previous value')
    matches[0]['publicationDate'] = correction['after']
    data['version'] = str(data.get('version', '1.0')) + '+erratum.20261029'
    data['appliedErrata'] = [correction['erratumId']]
    return data

if __name__ == '__main__':
    if len(sys.argv) != 3:
        raise SystemExit(__doc__)
    print(json.dumps(corrected_view(*sys.argv[1:]), ensure_ascii=False, indent=2))
