"""Read a supplied report CSV; do not infer live provenance from a file alone.
Evidence connectivity only. No ODRL interpretation, authorization engine, legal judgment, standards conformance or tamper resistance established.
No network calls or external actions.
"""
import csv, json, hashlib, sys
from pathlib import Path
REQUIRED=['decision_id', 'policy_id', 'receipt_id', 'resolved_policy', 'policy_revision', 'resolved_receipt', 'evaluator_version', 'evidence_state']
def blank(value): return value is None or value.strip().lower() in {'', 'null', '(null)'}
def transform(row):
    expected = 'policy_unknown' if blank(row['resolved_policy']) else 'receipt_missing' if blank(row['resolved_receipt']) else 'linked'
    if row['evidence_state'] != expected: raise ValueError('Evidence state does not match references')
    return {'id':row['decision_id'], 'policy':row['policy_id'], 'policy_revision':None if blank(row['policy_revision']) else row['policy_revision'], 'state':expected, 'evaluator_version':row['evaluator_version']}
def route(path):
    path=Path(path); records=[];seen=set()
    with path.open(encoding='utf-8-sig', newline='') as f:
        reader=csv.DictReader(f)
        names=reader.fieldnames or []
        normalized=[s.split('.')[-1].strip('" ') for s in names]
        if len(normalized)!=len(set(normalized)) or not set(REQUIRED).issubset(normalized): raise ValueError('Missing or ambiguous report columns')
        for raw in reader:
            row={new:raw[old] for old,new in zip(names,normalized)}
            key=row['decision_id']
            if not key or key in seen: raise ValueError('Missing or duplicate record ID')
            seen.add(key); records.append(transform(row))
    if not records: raise ValueError('Empty report cannot verify this fixture')
    return {'input_sha256':hashlib.sha256(path.read_bytes()).hexdigest(), 'source':'Supplied CSV; link console export evidence separately', 'external_actions_sent':False, 'records':records}
if __name__=='__main__':
    if len(sys.argv)!=2: raise SystemExit('Usage: python route-export.py report.csv')
    print(json.dumps(route(sys.argv[1]),indent=2))
