"""Re-implements the GCA branch of worker/src/routes/api.ts estimate for the full preset, with OpenRouter list prices."""
import json, subprocess, sys, math
repo=sys.argv[1]
T=json.loads(subprocess.run(['git','-C',repo,'show','624efc5:data/test/test_set.json'],capture_output=True,check=True).stdout)
gpt4o=(2.5e-6,10e-6); mini=(0.15e-6,0.6e-6)
target=gpt4o; judge=gpt4o
def jslen(s): return len(s.encode('utf-16-le'))//2
def c2t(n): return math.ceil(n/3.8)
def cost(p,i,o): return p[0]*i+p[1]*o
tot=dict(target=0,sim_env=0,sim_nsp=0,judge=0); toks=dict(tin=0,tout=0,sin=0,sout=0,jin=0,jout=0)
for c in T:
    profile=sum(jslen(p) for p in c['character_profiles'].values())+jslen(c['scenario'])
    ref=sum(jslen(d['character'])+jslen(d['message'])+4 for d in c['dialogues'])
    plot=jslen(c['plot']['summary'])
    lines,lineTok=14,70
    sys_=c2t(profile)+350
    for i in range(lines):
        h=sys_+i*lineTok
        if i%4==3:
            tot['sim_env']+=cost(gpt4o,h,60); toks['sin']+=h; toks['sout']+=60
        else:
            tot['target']+=cost(target,h,90); toks['tin']+=h; toks['tout']+=90
        tot['sim_nsp']+=cost(mini,h,40); toks['sin']+=h; toks['sout']+=40
    jin=c2t(profile+ref+plot)+1100+lines*lineTok
    n=4*1.15
    tot['judge']+=cost(judge,jin,450)*n; toks['jin']+=jin*n; toks['jout']+=450*n
k=3
print({a:round(b*k,2) for a,b in tot.items()},'TOTAL',round(sum(tot.values())*k,2))
print({a:round(b*k) for a,b in toks.items()})
