"""Recompute the published hypothetical allocation; no network or dispatch."""
import copy,json
from pathlib import Path
from router_core import candidates
HERE=Path(__file__).resolve().parent
source=json.loads((HERE/'input.json').read_text())
expected=json.loads((HERE/'results.json').read_text())
now=source['observed_at']
for row in expected['results']:
 for policy,name in [('baseline','baseline'),('concentrate','candidate')]:
  result=row[name];size=result['demand_pp'];options=[]
  for account in source['accounts']:
   aid=account['id']
   options.append(dict(id=aid,account=aid,pool=aid,model='claude-opus-5',provider='anthropic',route='claude-seat',held=False,ownership_clear=True,capabilities=['tools'],model_verified_at=now,identity_verified_at=now,billing_verified_at=now,billing_mode='included_only',machine=dict(id='hypothetical-fit',state='OK',reachable=True,observed_at=now,load_per_core=.2,memory_used_percent=40,fit='FITS_FLOOR',new_slots=100),windows=[dict(**w,observed_at=now,demand=size,uncertainty=1) for w in account['windows']]))
  reservations=[];touched=set()
  for i in range(source['task_counts'][row['split']]):
   snapshot=dict(schema='jev-route-input/v1',observed_at=now,task=dict(work_id=f'task-{i}',segment_id='simulation',origin='worker',fixed_model='claude-opus-5',held=False,required_capabilities=['tools']),options=options)
   choice=candidates(snapshot,reservations,policy=policy,now=now)['selected']
   if choice:
    touched.add(choice['account']);reservations.append(dict(state='reserved',pool=choice['pool'],machine=choice['machine'],demand=choice['demand']))
  weekly=[o['windows'][1]['remaining']-sum(r['demand']['seven_day'] for r in reservations if r['pool']==o['pool']) for o in options]
  actual=dict(admitted=len(reservations),accounts_touched=len(touched),highest_weekly_remaining=max(weekly),final_weekly_percent=weekly)
  for k,v in actual.items():assert result[k]==v,(row['split'],size,policy,k,v,result[k])
  print(row['split'],size,policy,actual)
print('All 18 policy/scenario results match the published numbers.')
