"""Immutable evidence and separate human reference overlays."""
import bootstrap
from pathlib import Path
import hashlib
import json

OUT=Path('/out')


def read(path):return json.loads(Path(path).read_text())


def sha(path):
    h=hashlib.sha256()
    with Path(path).open('rb') as f:
        for block in iter(lambda:f.read(1024*1024),b''):h.update(block)
    return h.hexdigest()


def lines(path):
    with Path(path).open() as f:
        for line in f:yield json.loads(line)


def paths(clip):
    s=read(OUT/'sources.json')[clip];root=Path('/evidence')/s['source_run']
    f=root/s['feature'];d=root/s['diagnostic']
    assert sha(f)==s['feature_sha256'] and sha(d)==s['baseline_sha256']
    return f,d,s


def reference(clip,row,feedback):
    rv=row.get('review_frame');matched=[]
    if rv is not None:
        matched=[o['id'] for o in feedback['negative_overlays'] if o['review_bounds'][0]<=rv<=o['review_bounds'][1]]
    if matched or clip=='SELF_C22':return 'negative_reference',matched
    return row['reference'],matched
