from pathlib import Path
from fractions import Fraction as F
import hashlib,json,re

ROOT=Path('/workspace/scratch/1b40da62dcbd')
source=ROOT/'project_sources/14-file_70-supplement-a-key-of-23-fine-resolution-720-30-rail-2-.md'
raw=source.read_bytes();lines=raw.decode().splitlines()
first=next(i for i,s in enumerate(lines) if s.startswith('## 1. '))
stop=next(i for i,s in enumerate(lines) if s.startswith('## 7. '))
sections=[];tables=[];section=None;i=first
while i<stop:
    s=lines[i]
    if s.startswith('## ') or s.startswith('### '):
        section=s.lstrip('# ');sections.append({'heading':section,'line':i+1})
    if s.startswith('|'):
        start=i;block=[]
        while i<stop and lines[i].startswith('|'):
            block.append(lines[i]);i+=1
        tables.append({'section':section,'start_line':start+1,'end_line':i,'literal_markdown':'\n'.join(block)})
        continue
    i+=1

def phase(year,suffix):return F(year)-(F(1,2) if suffix=='t' else 0)
E,P,J=F(25,23),F(70,69),F(300,299)
native=[4326,4296,3606,3576,2886,2856,2166,2136,1446,1416]
opened=[4336,4306,3606,3576,2886,2856,2166,2136,1436,1406]
data={
 'purpose':'Source preparation for C632–C731; no numbered step completed.',
 'source':{'path':str(source),'sha256':hashlib.sha256(raw).hexdigest(),'line_interval':[first+1,stop],
           'scope':'File_70 Supplement A §§1–6; node register consulted only for classification'},
 'constraints':{'primers':'quarantined; no primer-derived endpoint introduced','excluded_anchor':1486,
                'second_decimal_inverse':'deferred','SKL':'reproduce admitted source relations only; no new transports',
                'source_mutations':False},
 'sections':sections,'literal_source_tables':tables,
 'operators':{'E':'25/23','P':'70/69','J':'300/299','BC_n_coordinate':'X','BC_t_coordinate':'X-1/2',
              'civil_cross_axis_span':'BC+AD-1','PhaseNorm40':'E(69/2)+5/2=40; two-stage scalar definition, not a fourth Key'},
 'native_rail':{
   'source':'§§1.1–1.5, lines519–664','carrier':720,'rail':30,'crossed_core':690,
   'core_table':[{'state':'Native','core':690,'gain':0,'flank':720,'bracket':750},
                 {'state':'Prophetic','core':700,'gain':10,'flank':730,'bracket':760},
                 {'state':'Priestly','core':750,'gain':60,'flank':780,'bracket':810}],
   'ordered_BC_nodes':native,'ordered_gaps':[a-b for a,b in zip(native,native[1:])],
   'same_rail_BC_nodes':[native[::2],native[1::2]],'whole_width':2910,
   'inner_cell_BC_nodes':[3606,3576,2886,2856,2166,2136],'inner_width':1470,
   'Long_center':{'n':2871,'t':2872},'inner_half_width':735,'source_rail_half_width':15,
   'node_classes':{
    '4326/4296':'dependency-controlled Rounded MT comparison head and companion; not Actual baseline Creation',
    '3606/3576':'dependency-controlled SKL/Enoch/MT comparison nodes in this admitted field',
    '2886/2856':'source Long +30/base terminal anchors; retain SKL class',
    '2166':'dependency-controlled regular Abraham birth','2136':'Supplement-generated rail companion',
    '1446':'historical-chronology Exodus anchor','1416':'generated rail companion'}},
 'calendar_spectrum':{
   'source':'§§2.1–2.3, lines671–744',
   'doubles':[{'span':720,'unit':360,'register':'Prophetic'},
              {'span':722,'unit':361,'register':'19-squared calendrical body; not a solar-year definition'},
              {'span':728,'unit':364,'register':'Enochian'},
              {'span':730,'unit':365,'register':'common solar'}],
   '722_addition':{'nodes_BC':[3608,2936,2886],'parts':[672,50],'unit_parts':[336,25],
                   'status':'existing additive comparison; no new SKL transport'},
   'intercalation':{'six_blocks':2190,'final_block':1470,'six_block_anatomy':[6,360,30],
                    'final_anatomy':[4,360,30],'total':14610,'solar_years':40,'solar_unit':'1461/4',
                    'status':'File12 reconstructed cycle; downstream years-of-years comparison'},
   'atonement_comparisons':[[390,390],[690,780],[720,750]]},
 'outer_opening':{
   'source':'§3.2, lines761–786','ordered_BC_nodes':opened,
   'ordered_gaps':[a-b for a,b in zip(opened,opened[1:])],
   'changed_cores':[[4296,3606,4306,3606],[2136,1446,2136,1436]],
   'new_whole_width':2930,'gains':[10,10],
   'status':'declared local generated opening toward regular-MT Creation/Conquest fields; no universal actual-minus-rounded offset'},
 'phase_centers':[
   {'source':'§4.1','t':2872,'n':2871,'role':'Long corridor and lower normalized bracket','half_widths':[15,735,1470,20]},
   {'source':'§4.1','t':2922,'n':2921,'role':'parallel Short ladder','half_widths':[15,735]},
   {'source':'§4.1','t':2952,'n':2951,'role':'upper translated normalized bracket','half_widths':[20]},
   {'source':'§4.1','t':2912,'n':2911,'role':'schematic Enoch70 and generated120 common center','half_widths':[35,60]}],
 'scalar_and_coordinate_objects':{
   'source':'§4.2, lines817–880','scalar':{'core':690,'half_phase':'69/2','half_field':'1449/2',
      'P_completed_half':735,'half_gain':'21/2','central_gap':21,'whole':1470},
   'coordinate':{'center_n':2871,'start_n':[2884,2858],'center_offsets':[13,-13],
      'outward_span':'1449/2','result_t':[3609,2134],'full_center_arm':'1475/2','central_gap':26,'whole':1475},
   'crossed_728_pairs_BC':[[2884,2156],[3586,2858]],'728_anatomy':[700,15,13]},
 'PhaseNorm40':{
   'source':'§4.3, lines882–934',
   'pre':{'upper':{'t':2889,'n':2888},'lower':{'t':2855,'n':2854},
      'widths':{'t_to_t':34,'n_to_n':34,'outer_t_to_n':'69/2','inner_n_to_t':'67/2'}},
   'scalar':{'input':'69/2','P_output':35,'E_output':'75/2','residual':'5/2','normalized':40},
   'post':{'upper':{'t':2892,'n':2891},'lower':{'t':2852,'n':2851},
      'widths':{'t_to_t':40,'n_to_n':40,'outer_t_to_n':'81/2','inner_n_to_t':'79/2'}},
   'coordinate_outward_each':3,'recovered_35_labels':{'t':[2889,2854],'n':[2889,2854]},
   'status':'defined generated phase construction; scalar input and corresponding-suffix output are different measured objects'},
 'centered_70_120':{
   'source':'§§4.4–4.6, lines936–1011','Enoch70':{'t':[2947,2912,2877],'n':[2946,2911,2876],
      'gaps':[35,35],'outer_cross_phase':'141/2','inner_cross_phase':'139/2',
      'status':'dependency-controlled schematic generation, not literal historical biography'},
   'generated120':{'t':[2972,2932,2892,2852],'n':[2971,2931,2891,2851],
      'gaps':[40,40,40],'outer_cross_phase':'241/2','inner_cross_phase':'239/2'},
   'bracket_translation':{'parts':[50,30],'total':80,'status':'already admitted Supplement translation; no generalized SKL permission'},
   'completed_partition':{'t':[2972,2947,2912,2877,2852],'n':[2971,2946,2911,2876,2851],
      'gaps':[25,35,35,25],'E_outer_seed':23,'P_central_seed':69,
      'status':'additive composition; not a single-Key image of one full120 precursor'}},
 'closure_and_continuation':{
   'source':'§§5.1–5.4, lines1018–1103',
   '2930_pair':{'t':[4337,1407],'n':[4336,1406]},
   'outward_each':{'declared_local_Gear':2,'Priestly_gain':3,'total':5},
   '2940_pair':{'t':[4342,1402],'n':[4341,1401]},'center':{'t':2872,'n':2871},
   '2940_status':'generated extremities; not promoted Creation/Conquest anchors; local2 is not upstream transport authority',
   'historical_comparison_ladder':[{'era':'BC','year':1446,'role':'Exodus source anchor'},
      {'era':'BC','year':1416,'role':'generated rail companion'},
      {'era':'BC','year':726,'role':'schematic Samaria comparison'},
      {'era':'BC','year':696,'role':'dependency-controlled Hezekiah seam'},
      {'era':'BC','year':6,'role':'source Christ birth comparison'},
      {'era':'AD','year':25,'role':'Christological comparison terminal'}],
   'gaps':[30,690,30,690,30],'civil_total':1470,
   'native_bracket_pairs':[{'BC':726,'AD':25,'span':750},{'BC':724,'AD':27,'span':750}],
   'AD_terminal_extension':{'from':27,'gain':3,'to':30,'mechanism':'E(34.5)-34.5',
      'status':'source Christological phase/Gear comparison; no new historical event assignment'}},
 'micro_macro_fractal':{
   'source':'§§6.1–6.4, lines1114–1182',
   'micro':{'Enochian_years':'69/2','days_per_year':364,'root_days':12558,
      'routes':[{'operations':['P on34.5','multiply by360'],'result_days':12600},
                {'operations':['J on12558 days'],'result_days':12600},
                {'operations':['P on12558 days'],'result_days':12740}],
      'completed_forms':[[35,360,12600],[35,364,12740]]},
   'macro':{'Actual_completion_Exodus_BC':[14004,1446],'Actual_span_years':12558,
      'Rounded_Creation_Conquest_BC':[14006,1406],'Rounded_span_years':12600,
      'J_completed_scalar':12600,'P_completed_scalar':12740,
      'carrier_unit':'years-of-years, not days; source pairs have different endpoint roles'},
   'common_product':{'factors':[[12600,364],[12740,360]],'value':4586400,
      'status':'unit-qualified register product; not a third elapsed chronology'},
   'claim':'scale-homology / derived structural comparison, not unit identity'},
 'derived_preparation_checks':{
   'twentyfold_scalar_link':{'scale':20,'micro':['69/2',35,'75/2'],'macro':[690,700,750],
      'status':'derived from source values; not an additional source declaration'},
   'three_calendar_routes':{'input':'69/2','P_times360':12600,'J_times364':12600,'E_times336':12600,
      'status':'third Priestly volume route is a proposed derived synthesis; first two stated in§6.1'},
   'normalized_Priestly_volume':{'40_times336':13440,'residual_volume':840,
      'status':'derived scalar diagnostic; no endpoint allocated'},
   'phase_withdrawal':{'post_T_inward_2_5':'2889n/2854n','post_N_inward_2_5':'2889t/2854t',
      'status':'derived coordinate explanation of source recovered35 labels; suffix changes follow half-year coordinates'}}}

assert [a-b for a,b in zip(native,native[1:])]==[30,690,30,690,30,690,30,690,30]
assert [a-b for a,b in zip(opened,opened[1:])]==[30,700,30,690,30,690,30,700,30]
assert E*690==750 and P*690==700
assert E*F(69,2)==F(75,2) and P*F(69,2)==35
assert P*F(1449,2)==735 and 2*(13+F(1449,2))==1475
assert phase(3609,'t')-phase(2134,'t')==1475
assert phase(2892,'t')-F(5,2)==phase(2889,'n')
assert phase(2852,'t')+F(5,2)==phase(2854,'n')
assert phase(2891,'n')-F(5,2)==phase(2889,'t')
assert phase(2851,'n')+F(5,2)==phase(2854,'t')
assert 6*(6*360+30)+(4*360+30)==14610==40*F(1461,4)
assert F(69,2)*P*360==F(69,2)*E*336==F(69,2)*J*364==12600
assert 12558*J==12600 and 12558*P==12740
assert 12600*364==12740*360==4586400
out=ROOT/'c632_c731/prep/suppA_micro_inputs.json'
out.write_text(json.dumps(data,indent=2,ensure_ascii=False)+'\n')
print(json.dumps({'output':str(out),'literal_tables':len(tables),'sections':len(sections),'source_sha256':data['source']['sha256']}))
