"""BBTN forecast diagnostics. Python 3.9+; standard library only.

Run: python audit.py history-audit.csv
Inputs: date,game_id,pt,jacob,market,actual_margin,jacob_total,market_total,actual_total.
All margins are HOME VICTORY MARGINS. Market is the baseline at decision time.
Rows must be unique games; never count repeated snapshots as independent games.
"""
import csv,json,math,statistics,sys

def metrics(rows,key,target):
    errors=[float(r[target])-float(r[key]) for r in rows if r.get(target) not in ('',None) and r.get(key) not in ('',None)]
    if not errors:return {'n':0}
    return {'n':len(errors),'bias_actual_minus_forecast':statistics.mean(errors),'mae':statistics.mean(map(abs,errors)),'rmse':math.sqrt(statistics.mean(e*e for e in errors))}

def correlation(x,y):
    if len(x)<3:return None
    xm,ym=statistics.mean(x),statistics.mean(y)
    denom=math.sqrt(sum((v-xm)**2 for v in x)*sum((v-ym)**2 for v in y))
    return sum((a-xm)*(b-ym) for a,b in zip(x,y))/denom if denom else None

def audit(rows):
    seen=set()
    for r in rows:
        identity=r.get('game_id')
        if not identity or identity in seen:raise ValueError('Missing or duplicate game_id: use one predeclared decision-time observation per game.')
        seen.add(identity)
        if not r.get('date'):raise ValueError('Every record requires a date.')
        for k in ('pt','jacob','market','actual_margin','jacob_total','market_total','actual_total'):
            if r.get(k) not in ('',None) and not math.isfinite(float(r[k])):raise ValueError('Nonfinite numeric input: '+k)
    paired=[r|{'blend':(float(r['pt'])+float(r['jacob']))/2} for r in rows if all(r.get(k) not in ('',None) for k in ('pt','jacob','market','actual_margin'))]
    days=sorted({r['date'] for r in rows})
    result={'classification':'Forecast diagnostics only; no calibrated probabilities or profitability claim','games':len(rows),'distinct_dates':len(days),'paired_spreads':{k:metrics(paired,k,'actual_margin') for k in ('pt','jacob','blend','market')},'totals':{k:metrics(rows,k,'actual_total') for k in ('jacob_total','market_total')},'by_date':{d:{k:metrics([r for r in paired if r['date']==d],k,'actual_margin') for k in ('pt','jacob','blend','market')} for d in days}}
    errors=lambda k:[float(r['actual_margin'])-float(r[k]) for r in paired]
    result['paired_error_correlation_pt_jacob']=correlation(errors('pt'),errors('jacob'))
    result['limitations']=['Input provenance and final scores require independent verification.','Market comparisons require identical information cutoffs; closing lines cannot replace an earlier market input.','The report does not infer ROI from forecast accuracy. Exact odds, fees, settlement rules and recorded decisions are required.','Model-error correlation and bias from small samples may be unstable.']
    verified=all(r.get('prospective_verified','').lower()=='true' for r in rows)
    if not verified or len(days)<30:
        result['chronological_weight_test']={'status':'unavailable','reason':'Requires documented pregame provenance for every row and at least 30 distinct dates. This operational minimum does not guarantee adequate statistical power.'}
    else:
        # Train weights on the first 60% of dates; subsequent 20% is a diagnostic
        # validation window; final 20% is untouched test. Never tune on test.
        a,b=int(.6*len(days)),int(.8*len(days))
        train=[r for r in paired if r['date'] in days[:a]]
        if not train:raise ValueError('No paired training observations.')
        candidates=[i/20 for i in range(21)]
        loss=lambda w:sum((float(r['actual_margin'])-(w*float(r['pt'])+(1-w)*float(r['jacob'])))**2 for r in train)
        weight=min(candidates,key=loss)
        result['chronological_weight_test']={'status':'diagnostic only','pt_weight_fit_on_training':weight,'jacob_weight':1-weight,'cutoffs':{'training_last':days[a-1],'validation_last':days[b-1],'test_first':days[b]},'windows':{}}
        for name,dates in [('training',days[:a]),('validation',days[a:b]),('test',days[b:])]:
            subset=[r|{'fitted_blend':weight*float(r['pt'])+(1-weight)*float(r['jacob'])} for r in paired if r['date'] in dates]
            result['chronological_weight_test']['windows'][name]={k:metrics(subset,k,'actual_margin') for k in ('pt','jacob','blend','fitted_blend','market')}
        result['limitations'].append('Inspect results once using predeclared criteria; repeated changes after viewing test results invalidate its holdout status.')
    result['probability_calibration']='unavailable: no validated probability forecasts supplied'
    result['roi_interval']='unavailable: no complete executed-price/stake ledger or validated cluster analysis supplied'
    return result

if __name__=='__main__':
    if len(sys.argv)!=2:raise SystemExit('Usage: python audit.py records.csv')
    with open(sys.argv[1],newline='',encoding='utf-8-sig') as f:print(json.dumps(audit(list(csv.DictReader(f))),indent=2,allow_nan=False))
