"""Read a supplied report CSV; do not infer live provenance from a file alone.
Synthetic report time 100; window 10. One deterministic observation per service in this fixture. Freshness is not availability. No live transit feed or native GTFS adapter.
No network calls or external actions.
"""
import csv, json, hashlib, sys
from pathlib import Path
REQUIRED=['service_id', 'catalog_revision', 'observed_minute', 'observation_state', 'freshness']
def blank(value): return value is None or value.strip().lower() in {'', 'null', '(null)'}
def transform(row):
    seen = None if blank(row['observed_minute']) else int(row['observed_minute'])
    expected = 'no_observation' if seen is None else 'stale' if seen < 90 else 'current'
    if seen is not None and seen > 100: raise ValueError('Future observation needs a separate policy')
    if row['freshness'] != expected: raise ValueError('Freshness does not match the report-time policy')
    return {'id':row['service_id'], 'observed_minute':seen, 'freshness':expected, 'observation_state':None if blank(row['observation_state']) else row['observation_state']}
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['service_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))
