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

run('Choose source-generated connections over another endpoint scan',
    'Which fixed objects and operations can explain the new Flood and Abram endpoints without widening the mirror study?',
    lambda: model('scope',{'MSS':['MT','LXX','SP'],'modes':['Regular','Cumulative'],'anchors_BC':[1406,6],'source_targets':['536 BC','1446 BC','1406 BC','AD 65'],'Rounded_endpoint':'AD 2166','permitted':['one-pass reversal of original legs','ordered reconstruction','translation of named node packets','exact anchored Keys already admitted'],'deferred':['second decimal reversal','deeper mirror study','Gear transport to Creation'],'status':'synthesis of known source information; not a holdout'}),
    'The new task is a comparison of named paths and source differences. The three supplied spans are fixed targets for explanation, not results of a new search.',
    'Start with the inherited post-Flood rows: they may explain the 910 and 120 differences entering the inverse Floods.',
    ['Strategy §§1,5B–F,6–9','Reader_before.md','author instruction'])

def rounded_rows():
    raw=json.loads((ROOT/'evidence/postflood_rows_inherited.json').read_text())
    names=['Arphaxad','2nd Cainan','Shelah','Eber','Peleg','Reu','Serug','Nahor','Terah']
    keys={'MT':'MT_native_OFF','LXX':'LXX_native_ON','SP':'SP_native_OFF_official145'}
    out={}
    for m,key in keys.items():
        source={v['name']:v['L'] for v in raw[key]}
        out[m]={'names':names,'actual':[source.get(n,0) for n in names],'rounded':[5*((source.get(n,0)+2)//5) for n in names]}
    out['totals']={m:sum(out[m]['rounded']) for m in keys}
    assert out['totals']=={'MT':2405,'LXX':3315,'SP':2285}
    out['shared_downstream']=2431-1406
    out['rejected_precheck_assumption']='The shared downstream suffix was provisionally guessed as1000; row reconstruction and Abraham2431 require1025.'
    assert out['shared_downstream']==1025
    return model('postflood_rounded_rows',out)
run('Recover the rounded post-Flood lifespan suffix',
    'What do the controlled Arphaxad-through-Terah lifespan rows contribute before the shared Abraham–Moses suffix?',
    rounded_rows,
    'The rounded suffix totals are MT2405, LXX3315 and SP2285. Adding the source-controlled1025-year Abraham–Moses suffix produces3430,4340 and3310; the provisional1000 suffix was rejected.',
    'Decompose the LXX increment row by row; do not infer it from the endpoint910 alone.',
    ['postflood_rows_inherited.json','File18 §§2.2,3.2.1,4.2','File51a §16.2'])

def lxx_diff():
    r=model('postflood_rounded_rows');a=[x-y for x,y in zip(r['LXX']['actual'],r['MT']['actual'])];b=[x-y for x,y in zip(r['LXX']['rounded'],r['MT']['rounded'])]
    assert a==[27,460,27,40,100,100,100,60,0]
    assert b==[25,460,25,40,100,100,100,60,0]
    return model('LXX_910_source',{'names':r['MT']['names'],'actual_differences':a,'rounded_differences':b,'actual_total':sum(a),'rounded_total':sum(b),'Cainan':460,'other_rounded_rows':sum(b)-460,'rounding_change':sum(b)-sum(a)})
run('Resolve the 910 translation into named row changes',
    'Is the native LXX Flood displacement460+430, or does the rounded post-Flood source require a different decomposition?',
    lxx_diff,
    'At the Flood the910 is460+450, with450=25+25+40+100+100+100+60. Terah contributes zero. Rounding reduces the actual914 by4 in Arphaxad and Shelah.',
    'Keep430 for its proper later role; next identify the SP120 displacement from its own rows.',
    ['File18 §§2.2,4.2','postflood_rounded_rows.json'])

def sp_diff():
    r=model('postflood_rounded_rows');d=[x-y for x,y in zip(r['SP']['rounded'],r['MT']['rounded'])]
    assert d==[0,0,0,-60,0,0,0,0,-60]
    return model('SP_120_source',{'names':r['MT']['names'],'differences':d,'sum':sum(d),'Eber':-60,'Terah':-60})
run('Locate the SP source displacement',
    'Which local lifespan differences produce the120-year SP cumulative Flood displacement?',sp_diff,
    'SP Eber and official Terah each subtract60; all other shared post-Flood rounded lives agree with MT. Their120 reduction is inherited from source rows.',
    'Determine how far the MT–LXX910 and MT–SP120 translations propagate before the upper source lives change them.',
    ['File18 §§2.2,3.2.1','postflood_rounded_rows.json'])

def extent():
    nodes=json.loads((ROOT/'evidence/cumulative_source_nodes_inherited.json').read_text())
    plateau={n:{'LXX_minus_MT':nodes['LXX'][n]-nodes['MT'][n],'SP_minus_MT':nodes['SP'][n]-nodes['MT'][n]} for n in ['F','S','N']}
    assert all(v=={'LXX_minus_MT':910,'SP_minus_MT':-120} for v in plateau.values())
    pref=json.loads((ROOT/'evidence/prefix_source_rows_inherited.json').read_text())
    lamech=pref['rounded']['LXX'][-1]-pref['rounded']['MT'][-1]
    assert lamech==-20 and nodes['LXX']['C']-nodes['MT']['C']==890
    return model('source_translation_extent',{'Flood_Shem_Noah':plateau,'LXX_Lamech_difference':lamech,'LXX_creation_difference':890,'source_Creation_decomposition':[460,430],'source_Flood_decomposition':[460,450]})
run('Separate the Flood plateau from the Creation difference',
    'Does910 translate the whole MT–LXX cumulative genealogy, or only a named part of it?',extent,
    'Equal600-year Shem and950-year Noah lives preserve910 through those boundaries. LXX Lamech755 versus MT775 then reduces it to890=460+430 at Creation.',
    'The correct source explanation is local: now test which of these source differences survives the declared lower-leg reversal.',
    ['cumulative_source_nodes_inherited.json','prefix_source_rows_inherited.json','File18 §6'])
