"""Offline Decimal audit of31 public responses; --write publishes the reproducible projection."""
import argparse,datetime as dt,functools,gzip,hashlib,itertools,json,math,statistics
from decimal import Decimal as D,ROUND_HALF_EVEN,getcontext
from pathlib import Path
getcontext().prec=50
ROOT=Path(__file__).resolve().parents[3];EVIDENCE=Path(__file__).parent
INPUT=EVIDENCE/'2026-10-23-precision-requery-inputs.json.gz';DATA=json.loads(gzip.decompress(INPUT.read_bytes()))
VENUES=['Binance','Bybit','OKX','Bitget','Gate.io','KuCoin','MEXC','HTX'];STEP=28800000;GRID=D('.000001');FACTOR=D(109500)
def epoch(value):return int(dt.datetime.fromisoformat(value.replace('Z','+00:00')).timestamp()*1000)
def utc(t):return dt.datetime.fromtimestamp(t/1000,dt.timezone.utc).isoformat().replace('+00:00','Z')
def normalized(r):return str(r.normalize())
observations={v:{}for v in VENUES};responses=[]
for req in DATA['primaryResponses']:
 raw=req['bodyUtf8'];assert hashlib.sha256(raw.encode()).hexdigest()==req['sha256'];d=json.loads(raw,parse_float=str);v=req['venue']
 if v=='Binance':assert isinstance(d,list);rs=[(x['fundingTime'],x['fundingRate'])for x in d]
 elif v=='Bybit':assert d['retCode']==0;rs=[(x['fundingRateTimestamp'],x['fundingRate'])for x in d['result']['list']]
 elif v=='OKX':assert d['code']=='0';rs=[(x['fundingTime'],x['realizedRate'])for x in d['data']]
 elif v=='Bitget':assert d['code']=='00000';rs=[(x['fundingTime'],x['fundingRate'])for x in d['data']]
 elif v=='Gate.io':assert isinstance(d,list);rs=[(int(x['t'])*1000,x['r'])for x in d]
 elif v=='KuCoin':assert d['code']=='200000';rs=[(x['timepoint'],x['fundingRate'])for x in d['data']]
 elif v=='MEXC':assert d['success'] is True and d['code']==0;rs=[(x['settleTime'],x['fundingRate'])for x in d['data']['resultList']]
 elif v=='HTX':
  assert d['status']=='ok';assert all(x.get('realized_rate') is None for x in d['data']['data']);rs=[(x['funding_time'],x['funding_rate'])for x in d['data']['data']]
 for t,r in rs:
  t=int(t);r=str(r);assert D(r).is_finite()
  if t in observations[v]:assert D(observations[v][t])==D(r)
  observations[v][t]=r
 responses.append({k:req[k]for k in ['venue','group','url','at','status','sha256']})
shortStart=epoch('2026-09-28T00:00:00Z');longStart=epoch('2026-07-16T16:00:00Z');end=epoch('2026-10-14T08:00:00Z')
indices={};coverage=[]
for v,rows in observations.items():
 selected={}
 for t,r in rows.items():
  nominal=((t+STEP//2)//STEP)*STEP
  if longStart<=nominal<=end:
   assert abs(t-nominal)<=3000
   if nominal in selected:assert selected[nominal]['time']==t
   selected[nominal]={'time':t,'nominalTime':nominal,'rate':r,'offsetMs':t-nominal}
 indices[v]=selected;coverage.append({'venue':v,'availableInRequested270':len(selected),'missingInRequested270':270-len(selected),'maxOffsetMs':max(abs(r['offsetMs'])for r in selected.values())})
common=sorted(set.intersection(*(set(x)for x in indices.values())));assert len(common)==242 and all(b-a==STEP for a,b in zip(common,common[1:]))
short=[t for t in common if shortStart<=t<=end];assert len(short)==50

def analyze(times):
 spreads=[];before=after=extrema=firstWins=anchor=0;near=inclusive=exact=0;pairs={};counts={v:{'maxMember':0,'minMember':0}for v in VENUES}
 for t in times:
  row=[D(indices[v][t]['rate'])for v in VENUES];rounded=[r.quantize(GRID,rounding=ROUND_HALF_EVEN)for r in row];spreads.append((max(row)-min(row))*FACTOR)
  before+=len(set(row))<8;after+=len(set(rounded))<8;anchor+=all(r==D('.0001')for r in row)
  extrema+=({i for i,r in enumerate(row)if r==max(row)},{i for i,r in enumerate(row)if r==min(row)})!=({i for i,r in enumerate(rounded)if r==max(rounded)},{i for i,r in enumerate(rounded)if r==min(rounded)})
  firstWins+=(row.index(max(row)),row.index(min(row)))!=(rounded.index(max(rounded)),rounded.index(min(rounded)))
  for i,v in enumerate(VENUES):counts[v]['maxMember']+=row[i]==max(row);counts[v]['minMember']+=row[i]==min(row)
  for a,b in itertools.combinations(range(8),2):
   gap=abs(row[a]-row[b]);near+=gap<GRID;inclusive+=gap<=GRID;exact+=gap==0
   if gap<GRID:key=VENUES[a]+'/'+VENUES[b];pairs[key]=pairs.get(key,0)+1
 q=statistics.quantiles(spreads,n=4,method='inclusive')
 return {'n':len(times),'firstUtc':utc(times[0]),'lastUtc':utc(times[-1]),'tiesBefore':before,'tiesAfterHalfEven':after,'newlyTiedTimes':after-before,'extremaMembershipChanges':extrema,'firstWinsChangesForListedVenueOrder':firstWins,'allAtAnchor':anchor,'pairCount':len(times)*28,'pairsBelowOneStep':near,'pairsAtMostOneStep':inclusive,'exactPairTies':exact,'nearPairCounts':pairs,'extremaCountsIncludingAllTies':counts,'spreadMedianAnnualizedPoints':normalized(statistics.median(spreads)),'spreadQ1AnnualizedPoints':normalized(q[0]),'spreadQ3AnnualizedPoints':normalized(q[2]),'spreadRows':[[t,normalized(s)]for t,s in zip(times,spreads)],'rows':[{'nominalTime':t,'rates':{v:indices[v][t]['rate']for v in VENUES}}for t in times]}
shortData=analyze(short);longData=analyze(common);grids=[]
def median_spread(venues,times):
 return normalized(statistics.median((max(D(indices[v][t]['rate'])for v in venues)-min(D(indices[v][t]['rate'])for v in venues))*FACTOR for t in times))
shortData['sensitivity']={'withoutBitget':median_spread([v for v in VENUES if v!='Bitget'],short),'withoutHtx':median_spread([v for v in VENUES if v!='HTX'],short),'bitgetOffBaselineN':sum(D(indices['Bitget'][t]['rate'])!=D('.0001')for t in short),'bitgetOffBaselineMedian':median_spread(VENUES,[t for t in short if D(indices['Bitget'][t]['rate'])!=D('.0001')])}

for v in VENUES:
 values=[D(indices[v][t]['rate'])for t in short];decimals=max(max(0,-r.normalize().as_tuple().exponent)for r in values);scale=D(10)**decimals;integers=[int(abs(r)*scale)for r in values];gcd=D(functools.reduce(math.gcd,integers))/scale
 nonbase=[r for r in values if r!=D('.0001')]
 grids.append({'venue':v,'n':50,'gridMembers1e6':sum(r%GRID==0 for r in values),'maxObservedNormalizedDecimalPlaces':decimals,'sampleGcd':normalized(gcd),'sampleGcdAnnualizedPoints':normalized(gcd*FACTOR),'distinctBefore':len(set(values)),'distinctAfterHalfEven':len(set(r.quantize(GRID,rounding=ROUND_HALF_EVEN)for r in values)),'anchorCount':len(values)-len(nonbase),'nonAnchorN':len(nonbase),'nonAnchorDistinct':len(set(nonbase)),'latestRawNumericToken':indices[v][short[-1]]['rate'],'latestAnnualizedPercent':normalized(values[-1]*FACTOR)})
binance=sorted(indices['Binance']);loss=[]
for n in [25,50,100,150,200,270]:
 a=[D(indices['Binance'][t]['rate'])for t in binance[-n:]];b=[x.quantize(GRID,rounding=ROUND_HALF_EVEN)for x in a];loss.append({'n':n,'distinctBefore':len(set(a)),'distinctAfter':len(set(b)),'lostPercent':normalized((1-D(len(set(b)))/D(len(set(a))))*100)})
a=[D(indices['Binance'][t]['rate'])for t in short];b=[D(indices['MEXC'][t]['rate'])for t in short];ma=statistics.mean(a);mb=statistics.mean(b);corr=sum((x-ma)*(y-mb)for x,y in zip(a,b))/(sum((x-ma)**2 for x in a)*sum((y-mb)**2 for y in b)).sqrt()
hourly=[];names=['Binance','Bybit','Okx','Bitget','Hyperliquid','Deribit']
for row in DATA['hourly']['rows']:
 rates={v:normalized(D(row['fundingRate'+v])*100*(8760 if v=='Hyperliquid' else 1095))for v in names};spread=max(map(D,rates.values()))-min(map(D,rates.values()));hourly.append({'time':row['time'],'annualizedPercent':rates,'spreadAnnualizedPoints':normalized(spread)})
assert len(hourly)==168
ordered=sorted(D(x['spreadAnnualizedPoints'])for x in hourly)
result={'reviewDate':'2026-10-23','kind':'Historical requery; original50-point short claims compared, original230-point long sample NOT recreated','sourceInputSha256':hashlib.sha256(INPUT.read_bytes()).hexdigest(),'venueOrder':VENUES,'intervalMinutes':480,'annualizationMultiplierFractional8hRateToAnnualPercent':109500,'rounding':'Decimal nearest multiple1e-6, ties to even; preserves monotonic order but may add ties. No executable trading decision inferred.','timestampAlignment':'Nearest8h UTC only within3000ms; original timestamps preserved in source. Short-window Binance maximum7ms; long-window Binance26ms and Gate3000ms.','htxLimit':'funding_rate is computed; realized_rate is null in every retrieved record. Not proof of charged cash-flow rate.','coverage':coverage,'observedGrids':grids,'short':shortData,'longRequery':longData,'binanceWindowLoss':loss,'binanceMexc':{'correlation':normalized(corr),'medianGapGridSteps':normalized(statistics.median(abs(x-y)/GRID for x,y in zip(a,b))),'maxGapGridSteps':normalized(max(abs(x-y)/GRID for x,y in zip(a,b)))},'gcdCounterexample':{'assumedUnderlyingZeroAnchoredStep':'0.000001','observed':['0.000002','0.000004'],'sampleGcd':'0.000002','conclusion':'Sample GCD may be coarser than true zero-anchored step; finite membership does not establish minimum possible step or permanent policy.'},'hourlyRequery':{'n':168,'firstUtc':hourly[0]['time'],'lastUtc':hourly[-1]['time'],'median':normalized(statistics.median(ordered)),'quartileMethod':'Order statistics at zero-based floor(n*p) for p=.25,.75; same rule reconciles original reported quartiles. Median averages middle pair.','q1':normalized(ordered[42]),'q3':normalized(ordered[126]),'min':normalized(min(ordered)),'max':normalized(max(ordered)),'rows':hourly,'scope':'Stored hourly mean indicative rates, six-venue set, different window and contracts, shared underlying venue feeds. Not independent contemporaneous settlement capture; final-hour values differ from the published original snapshot; partial capture or later revision is possible but not proven.'},'sources':responses,'limits':['Original230 timestamps and full-history counts1520 lack recovered original dataset/script. This revision uses a new242point common intersection with explicit dates and acquisition date, no silent historical substitution.','Observed precision does not establish rounding versus truncation or an exchange-wide permanent policy.','Annualization is a comparison convention, not realized return. Rounding-error half-step assumes nearest rounding; RMS step/sqrt12 additionally assumes uniform residuals.','Historical market averages do not repair old production volume/interval unit defects or prove account funding payments.']}
assert shortData['tiesBefore']==8 and shortData['tiesAfterHalfEven']==24 and shortData['extremaMembershipChanges']==4 and shortData['pairsBelowOneStep']==60
assert longData['n']==242 and longData['tiesBefore']==78 and longData['tiesAfterHalfEven']==170 and longData['extremaMembershipChanges']==22
parser=argparse.ArgumentParser();parser.add_argument('--write',action='store_true');args=parser.parse_args()
if args.write:
 dest=ROOT/'products/athenum-analytics/apps/marketing/public/research/funding-precision-20261023.json';dest.write_text(json.dumps(result,ensure_ascii=False,indent=2)+'\n')
print(json.dumps({'short':{k:v for k,v in shortData.items()if k not in ['rows','spreadRows','nearPairCounts','extremaCountsIncludingAllTies']},'long':{k:v for k,v in longData.items()if k not in ['rows','spreadRows','nearPairCounts','extremaCountsIncludingAllTies']},'hourly':{k:v for k,v in result['hourlyRequery'].items()if k!='rows'},'sourceSha256':result['sourceInputSha256']},indent=2))
