#!/usr/bin/env python3
"""Close the two measured fixed-list numeric defects with actual Root959 HTTP."""
import hashlib
import json
from pathlib import Path
import subprocess
import sys
import integrate_20261010 as tool
from integrate_20261011_batch72 import seal, RUNNER
from integrate_20261011_batch74 import verify as verify_original
from integrate_20261011_batches40_42 import sha
from integrate_20261011_batches46_48 import item

BASE=tool.STORE+'parallel-store-onto-nutrition-read10-20261011/next-safe/'
FINAL=BASE+'candidate-root959/'
def verify():
    root=tool.ROOT/BASE;final=tool.ROOT/FINAL
    verify_original() # Pure sealed replay, no HTTP/database action or report writes.
    seal(root,'strict-candidate-manifest.json','de44ca74f2104f1baa6c36e16cd11a675238c6fe19895519152d4e4fdc162ae4',
        'c82d49edb2ac9ff49805f07df136aace5f95e81f81c57328c0b4911b638377a7',10)
    seal(final,'manifest.json','675f0a3007fb3307b3c36079886739fd391bb6a042429f1c4cd2fe0a3f99e1ce',
        '9647ffa857499c95d5eaceffa9a796ecb8e7f1b9b08f2191da80cd99155a2264',53)
    result=subprocess.run([sys.executable,'-B','-c',RUNNER,str(final)],capture_output=True,text=True)
    assert result.returncode==0,result.stderr
    report=json.loads(result.stdout)
    assert report['HTTP']==4 and report['DML']==report['DDL']==report['logs']==0
    assert report['assertions']==2489 and report['negative_cases']==6
    for method,r in report['compatibility_differences'].items():
        assert not r['body_equal'] # Natural counts/identities remain real, not washed away.
        assert r['nutrition_sum']['B']==150 and r['nutrition_sum']['C']==3
        assert not any(d['field'] in ['power','carbohydrate','protein','fat','sodium','use_weight'] for d in r['row_differences'])
    source=Path(__file__).with_name('STORE_APPLICATION_20261011_0836.json')
    assert sha(source)=='49b1a564de5a93dcd0385659a3d79f2e92256970b7fd7cc0a1adb8d482ccdd57'
    sources=json.loads(source.read_text());assert len(sources)==959
    assert hashlib.sha256(json.dumps(sources,sort_keys=True,separators=(',',':')).encode()).hexdigest()=='a6c3604ca13eddf6dce5c9d3e7c8ed61e56406164ccbff23971fc9d764f069c5'
    for relative,digest in sources.items():
        assert sha(tool.ROOT.parent/'zhctproject/store/application'/relative)==digest,relative

def targets():
    return [item('store','POST',f'/{module}/nutrition/lists',FINAL,
        risk='formal-owned-single-row-list-Root959-physical-MD-and-plain-FLOAT-types-native-proven',
        parity='partial-full-per-engine-DTO-oracles-two-MySQL-old-new-byte-identical-controls-numeric-defects-resolved-natural-counts-retained',
        boundary_empty_min_max='partial-fixed-physical-projection-only-no-all-dynamic-projection-or-legacy-data-claim',
        cleanup='own-worker-ports-Auth-cache-cleared-full-product-unchanged-lease-released-before-separate-approved-weight-update')
        for module in ['p','m']]

if __name__=='__main__':
    audit=Path(__file__).with_name('integration-20261011-batch75.json')
    assert not audit.exists(),'Preserve original transition audit'
    tool.main(targets(),audit,verify)
