import sys
from pathlib import Path
sys.path.insert(0,str(Path(__file__).resolve().parents[1]))
from research import *
from runner import run

def three_restoration():
    completed=model('SP_MT_Shem_condition')['completed_Floods']
    rows={m:{'Flood_BC':v,'to_restoration':v-536,'units70':(v-536)//70,'remainder70':(v-536)%70,'remainder490':(v-536)%490} for m,v in completed.items()}
    assert [rows[m]['to_restoration'] for m in ['MT','LXX','SP']]==[7000,7910,4900]
    assert all(v['remainder70']==0 for v in rows.values())
    return model('three_cumulative_restoration',rows)
run('Complete the three-manuscript restoration comparison',
    'What is the LXX member when the same completed inverse Flood construction is compared with the already-selected536 target?',three_restoration,
    'The whole fixed family is MT7000, LXX7910, SP4900:100,113,70 units of70. LXX is reported with its490 remainder70; it is not forced into the same Jubilee count.',
    'Explain why all three occupy this70-year grid despite the2700 displacement itself not being divisible by70.',
    ['SP_MT_Shem_condition.json','author7000/4900 observations'])

def compensation():
    rev=model('lower_spine_map')['reversed'];base={m:n//70 for m,n in rev.items()}
    common=2700+1406-536
    assert common==3570==51*70
    assert {m:n+51 for m,n in base.items()}=={'MT':100,'LXX':113,'SP':70}
    return model('restoration_compensation',{'lower_leg_indices':base,'stage_shift':2700,'Conquest_to_restoration':870,'common_addition':3570,'common_units70':51,'residues70':[2700%70,870%70],'formula':'completed Flood - 536 = I(F-1406) + 2700 + 870'})
run('Derive the common51-unit restoration addition',
    'What source-appointed anchor relation converts the49,62,19 lower-leg indices into100,113,70?',compensation,
    'The anchor displacement2700 and Conquest–restoration870 add to3570=51×70. Their residues40 and30 complete one70; the same addition explains all three rows.',
    'Now separate the between-manuscript distances from this common target placement.',
    ['lower_spine_map.json','three_cumulative_restoration.json'])

def grid_edges():
    d=model('three_cumulative_restoration');m=d['MT']['to_restoration'];l=d['LXX']['to_restoration'];s=d['SP']['to_restoration']
    assert (m-s,l-m,l-s)==(2100,910,3010)
    return model('restoration_grid_edges',{'ordered_indices':['LXX113','MT100','SP70','restoration0'],'adjacent_units70':[13,30,70],'LXX_SP_gap':3010,'LXX_SP_factorizations':['43×70','7×430'],'source_LXX_SP_gap':5746-4716,'SP_inverse_contraction':1980})
run('Join the Babylonian70 and Egypt430 measures in the same source gap',
    'How does the LXX–SP separation combine the independently derived910 and2100 differences?',grid_edges,
    'The LXX–SP inverse Flood gap is3010=910+2100=43×70=7×430. It comes from their1030 source gap plus the SP1980 contraction; it is one interval with two descriptions.',
    'Test the already-appointed Exodus target1446 on the LXX row rather than looking for a new target.',
    ['LXX_910_source.json','SP_120_source.json','lower_decimal_mechanism.json'])

def lxx_exodus():
    a=model('SP_MT_Shem_condition')['completed_Floods'];d=1446-536
    assert a['LXX']-a['MT']==d==910
    assert a['LXX']-1446==a['MT']-536==7000
    return model('LXX_MT_7000_translation',{'MT':[7536,536],'LXX':[8446,1446],'both_endpoints_translation_BC':910,'common_span':7000,'interpretation':'The source910 matches the selected Exodus–restoration910, so the7000 is transported as a complete interval.'})
run('Transport the7000 from restoration to Exodus',
    'Does the source-derivedMT–LXX910 map the new7000 interval to the selected Exodus anchor?',lxx_exodus,
    'MT7536→536 and LXX8446→1446 are exact910 translations and both7000. The LXX7910 to restoration becomes7000 by moving the target as well as the source node.',
    'Account for7910 within the LXX Regular/Cumulative relation, so the unsimplified third row also has a structural role.',
    ['Comparative_Study §4.1','three_cumulative_restoration.json'])

def lxx_530():
    reg=6+4100+inverse(1830);cum=6+4100+inverse(4340)
    assert (reg,cum)==(7916,8446) and cum-reg==536-6==530
    assert cum-536==reg-6==7910
    return model('LXX_530_target_transport',{'completed_regular':[7916,6],'completed_cumulative':[8446,536],'coordinate_difference':530,'common_span':7910,'held_regular':[5216,6],'held_cumulative':[5746,536],'held_common_span':5210})
run('Explain the LXX7910 through its Regular/Cumulative lower-leg difference',
    'Can the LXX restoration7910 be related to an existing source-appointed LXX Regular interval?',lxx_530,
    'The inverse Flood gap4340−3810=530 matches536−6. Thus LXX8446→536 and7916→6 both measure7910; the held-stage pair similarly preserves5210.',
    'The restoration family now has three explained members. Build the full named-node7000 packet next.',
    ['Comparative_Study §§3.1–3.3','File52c anchor6 convention'])
