"""Read T883 and check all stored values, including endpoint certificates.

Requires generate.py, checks.py, and entries.json beside this script.
Default invocation is read-only; the API key may be piped via stdin.
"""
import os,sys,json
from pathlib import Path
os.environ['NUMBERDB_API_KEY']=sys.stdin.read().strip()
import numberdb.sage as numberdb
from generate import InverseLi
from checks import certify
from sage.rings.integer_ring import ZZ
from numberdb._http import Client
c=Client(api_key=os.environ['NUMBERDB_API_KEY']);doc=numberdb.table('T883',client=c)
expected=json.loads((Path(__file__).parent/'entries.json').read_text())
assert doc['Parameters']['x']['type']=='R' and list(doc['Parameters'])==['x']
block=doc['Numbers']
if isinstance(block,dict):
 rows=[{'params':{'x':key},'number':entry if isinstance(entry,str) else entry['number']} for key,entry in block.items()]
else:rows=block
assert rows==expected,(len(rows),len(expected))
assert [r['params']['x'] for r in rows]==[str(n) for n in range(1,501)]
for row in rows:
 okay,unused=certify(row['number'],ZZ(row['params']['x']));assert okay
report=InverseLi().verify(sample=None);assert report.ok and report.checked==report.matched==500
result={'table':'T883','checked_entries':500,'total_entries':500,'stored_intervals_series_certified':500,
 'exact_row_order_and_strings_equal_offline':True,
 'verify':{'ok':report.ok,'checked':report.checked,'matched':report.matched,'differing':report.differing,'missing':report.missing,'extra':report.extra},
 'native_return_type':type(InverseLi().value({'x':1},100)).__name__,
 'skill_manifest':'../20261201T025022Z-build-skills/manifest.json'}
print('NUMBERDB_READBACK_JSON='+json.dumps(result))
