"""Check the one-point integrals named in T463's repaired comment.

Run with SageMath after installing numberdb:
    sage -python check_genus_one.py
Keep repair-inputs.json (the exact API excerpts) beside this script.

Source identities: Vakil, arXiv:math/0602347v2, section 3.14 (psi
normalization, including the generic involution), and Faber-Pandharipande,
arXiv:math/9908052, equations (4)-(5) (lambda_g one-point formula).
Macaulay2 HodgeIntegrals documentation independently prints both 1/24 values.
This checks the exact arithmetic and entry identities, not the geometric proofs.
"""
import json
from pathlib import Path
from math import factorial
from fractions import Fraction
import numberdb.sage as numberdb
from sage.rings.rational_field import QQ
from sage.arith.misc import bernoulli

root=Path(__file__).resolve().parent
def read(tid):
    return json.loads((root/'repair-inputs.json').read_text())[tid]['document']
def value(doc, *keys):
    obj=doc['Numbers']
    for key in keys:
        obj=obj[str(key)]
    raw=Fraction(obj['number'] if isinstance(obj,dict) else obj)
    return QQ(raw.numerator)/QQ(raw.denominator)

# Known-answer control for the Bernoulli implementation.
assert bernoulli(2)==QQ(1)/QQ(6)
# Vakil's pencil has 12 nodal fibers and stack degree 24.
psi=QQ(1)/QQ(12*2)
# The lambda_g formula gives b_1=(1-2^(1-2))*|B_2|/2!.
lam=(QQ(1)-QQ(1)/QQ(2))*abs(bernoulli(2))/QQ(factorial(2))
assert psi==lam==QQ(1)/QQ(24)
psi_doc=read('T458')
lam_doc=read('T462')
assert list(psi_doc['Parameters'])==['g','n','d']
assert list(lam_doc['Parameters'])==['g','n','d']
assert value(psi_doc,1,1,1)==psi
assert value(lam_doc,1,1,0)==lam
# A wrong value without the automorphism weight must fail both comparisons.
assert QQ(1)/QQ(12)!=value(psi_doc,1,1,1)
assert QQ(1)/QQ(12)!=value(lam_doc,1,1,0)
# Verify these integrals' ELSV specialization on T463's ten genus-one
# one-part profiles: H_(1,(d))=(d+1)! d^d/d! * (QQ(d)*psi-lambda).
hurwitz=read('T463')
results=[]
for d in range(1,11):
    computed=QQ(factorial(d+1))*QQ(d**d)/QQ(factorial(d))*(QQ(d)*psi-lam)
    stored=value(hurwitz,1,d)
    assert computed==stored,(d,computed,stored)
    results.append({'g':1,'mu':str(d),'value':str(stored)})
assert value(hurwitz,1,2)==QQ(1)/QQ(2)
print(json.dumps({'status':'passed','psi_integral':str(psi),'lambda_integral':str(lam),
                  'linked_entries_checked':['T458#1,1,1','T462#1,1,0'],
                  'known_answer_control':'B_2=1/6 and H_(1,(2))=1/2',
                  'wrong_1_over_12_rejected':True,
                  'genus_one_one_part_elsv_checks':results},indent=2))
