"""Read a supplied report CSV; do not infer live provenance from a file alone.
External file bytes remain external. Symbolic digest labels are not real hashes. No LAZ decoding, spatial indexing or geospatial correctness claim.
No network calls or external actions.
"""
import csv, json, hashlib, sys
from pathlib import Path
REQUIRED=['link_id', 'derivative_id', 'source_id', 'receipt_id', 'resolved_derivative', 'resolved_source', 'reference_state']
def blank(value): return value is None or value.strip().lower() in {'', 'null', '(null)'}
def transform(row):
    expected = 'missing_derivative' if blank(row['resolved_derivative']) else 'missing_source' if blank(row['resolved_source']) else 'resolved'
    if row['reference_state'] != expected: raise ValueError('Reference state does not match endpoint evidence')
    return {'link':row['link_id'], 'derivative':row['derivative_id'], 'source':row['source_id'], 'receipt':row['receipt_id'], 'state':expected}
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['link_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))
