"""Full-search shuffled calibration for two predeclared targets, not a cipher proof."""
import json,time
from combined_pass import *

if __name__=='__main__':
    rng=np.random.default_rng(20260918)
    sections=[s for s in '%'.join(lp[:56]).split('$') if len(vals(s))]
    saved=json.loads((ROOT/'combined_results.json').read_text())
    out={'seed':20260918,'shuffles_per_target':32,'targets':[],
         'interpretation':'Each shuffle reruns all stream, phase, reset, sign, shift and all-F/no-F selection using only the prefix. Permutation preserves rune counts and punctuation positions. Empirical p=(1+null>=observed)/(1+32); coarse calibration, not probability hypothesis is true. Targets chosen as opening section and section beginning after square.'}
    for target in (0,4):
        source=sections[target];c=vals(source)
        observed=next(s for s in saved['sections'] if s['target']==f'marked_section_{target}')['top_prefix_candidates'][0]['prefix_score']
        null=[]
        for i in range(32):
            it=iter(rng.permutation(c));shuffled=''.join(R[int(next(it))] if ch in R else ch for ch in source)
            null.append(sweep(shuffled,'shuffle',retain=False))
            if (i+1)%8==0:print('Target',target,'shuffles',i+1,flush=True)
        exceed=sum(x>=observed for x in null)
        item={'section_index':target,'observed_best_prefix_score':observed,'shuffled_best_prefix_scores':null,'null_at_least_observed':exceed,'empirical_p':(exceed+1)/33}
        out['targets'].append(item);print('RESULT',target,exceed,'of32',item['empirical_p'],flush=True)
    (ROOT/'combined_null_results.json').write_text(json.dumps(out,indent=2))
