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

def rounded_path():
    orig=model('three_MSS_12600_path')['nodes_BC'];q=[1-b for b in orig];moved=[v+2700 for v in q]
    labels=[{'era':'BC' if x<0 else 'Rounded AD','year':abs(x)+1} for x in moved]
    assert labels==[{'era':'BC','year':10436},{'era':'BC','year':4836},{'era':'BC','year':2736},{'era':'Rounded AD','year':2166}]
    assert [b-a for a,b in zip(moved,moved[1:])]==[5600,2100,4900]
    return model('three_MSS_translated_path',{'original_q':q,'translated_q':moved,'labels':labels,'edges':[5600,2100,4900],'whole':12600,'shift':2700,'status':'whole-path form of the authorized C1584 relation'})
run('Transport the whole common refinement to Rounded AD2166',
    'Does the authorized2700 shift preserve the newly identified internal MT/SP cuts as well as the three total spans?',rounded_path,
    'Yes:10436→4836→2736→Rounded AD2166 retains5600|2100|4900. The7000 and12600 totals are coarsenings of that same translated path.',
    'Identify its connection to the existing civil Jubilee chain at the shared MT Flood node.',
    ['three_MSS_12600_path.json','File52c §1.4','C1584'])

def junction():
    rounded=4836+2166-2;civil=4836+65-1
    assert (rounded,civil,rounded-civil)==(7000,4900,2100)
    return model('4836_junction',{'node_BC':4836,'roles':['ordinary Rounded MT cumulative Flood','Conquest-held MT inverse cumulative Flood','2700-forward image of completed MT inverse Flood'],'Rounded_AD2166_width':rounded,'civil_AD65_width':civil,'difference_of_declared_widths':2100,'guard':'Two separately declared coordinate displays; no new reflection or concatenated mixed-label path.'})
run('Locate the shared node connecting the two endpoint families',
    'Where does the translated Babylonian/Abram packet meet the SP Jubilee comparison?',junction,
    'They meet at MT4836: it is7000 to Rounded AD2166 and4900 to civil AD65. The new connection uses an established node shared by the two declared comparisons.',
    'Explain the SP9800 and11270 from its original reversed prefix, keeping the civil AD65 convention.',
    ['C1583','three_MSS_translated_path.json'])

def sp_prefix():
    p=inverse(1400)+inverse(1700)
    assert p==11200==8*1400==16*700
    assert p-1400==9800==20*490
    assert p+(6+65-1)==11270==23*490
    return model('SP_Jubilee_prefix',{'reversed_prefix':[4100,7100],'sum':p,'units_original_1400':8,'to_Conquest':p-1400,'to_AD65_civil':p+70,'Conquest_to_AD65':1470,'origin':'SP source Flood3106−Conquest1406=1700 and Nativity6−Conquest span1400'})
run('Derive both SP Jubilee observations from one reversed prefix',
    'What common source calculation generates9800 to Conquest and11270 to AD65?',sp_prefix,
    'SP4100+7100=11200=8×1400. Subtracting one1400 gives9800; adding the civil70 from6 BC to AD65 gives11270.',
    'Compare the whole fixed six-spine Flood family so that the scope of the SP success is explicit.',
    ['Comparative_Study §7.2','C1583'])

def fixed_flood_table():
    legs={'MT Regular':1050,'MT Cumulative':3430,'LXX Regular':1830,'LXX Cumulative':4340,'SP Regular':1700,'SP Cumulative':3310};out=[]
    for name,leg in legs.items():
        for stage in ['held','completed']:
            f=1406+inverse(leg)+(2700 if stage=='completed' else 0)
            spans={'6 BC':f-6,'1406 BC':f-1406,'AD65 civil':f+65-1}
            out.append({'family':name,'stage':stage,'Flood_BC':f,'spans':spans,'remainders490':{k:v%490 for k,v in spans.items()},'remainders70':{k:v%70 for k,v in spans.items()}})
    hit=[r['family'] for r in out if r['stage']=='completed' and r['remainders490']['1406 BC']==r['remainders490']['AD65 civil']==0]
    assert hit==['SP Regular']
    return model('fixed_Flood_target_table',{'nodes':out,'completed_Jubilee_pair_hits':hit,'node_count':12,'comparison_count':36,'scope':'fixed nodes and three already-appointed targets; diagnostic, not a probability experiment'})
run('Compare the two Jubilee targets across every primary Flood spine',
    'Is the SP pair shared by all six primary spines under the same two anchor stages?',fixed_flood_table,
    'Among completed primary Floods, SP Regular alone gives exact490 multiples to both1406 BC and AD65. The complete12-node/36-span table retains all other values and remainders.',
    'Determine how much of the paired target success is forced by the relation between the targets.',
    ['Comparative_Study source register','SP_Jubilee_prefix.json'])

def target_dependence():
    rows=model('fixed_Flood_target_table')['nodes']
    for r in rows:
        assert r['spans']['AD65 civil']-r['spans']['1406 BC']==1470
        assert len(set(r['remainders70'].values()))==1
        assert r['remainders490']['AD65 civil']==r['remainders490']['1406 BC']
    completed={r['family']:r['remainders70']['6 BC'] for r in rows if r['stage']=='completed'}
    assert completed['MT Regular']==10 and completed['LXX Regular']==completed['SP Regular']==0
    return model('Jubilee_target_dependence',{'target_separation':1470,'Jubilees':3,'completed_remainders70':completed,'paired490_condition':'Both490 divisibilities are the same residue test because the targets differ by3×490.','three70_condition':'6 BC,1406 BC andcivil AD65 belong to one70-year target class.'})
run('Separate the SP source constraint from repeated target counts',
    'Are the9800 and11270 divisibilities independent numerical conditions?',target_dependence,
    'Their common source placement is substantive, but the second490 divisibility follows from the first because Conquest→AD65 is3×490. The same70 residue persists across all three selected targets.',
    'Use the inherited MT7-Jubilee Flood leg to explain the internal13|7|3 partition.',
    ['fixed_Flood_target_table.json','Strategy §§5D,6'])
