#!/usr/bin/env python3
"""Five nonempty nutrition reads; unlike snapshots use independent DTO truth."""
import json
from pathlib import Path
import subprocess
import sys
import integrate_20261010 as tool
from integrate_20261011_batches40_42 import private_files, run, sha
from integrate_20261011_batches46_48 import item

BASE=tool.STORE+'parallel-store-nutrition-safe-read5-20261011/'
PINS={'original-manifest.json':'5a11e74ba560cde12fa6417fb5337d117ab93a47f53db9dad73effe2ade1548a',
      'final-manifest.json':'a57879e6587471c86e2f2a74660a7634d5f04e8ba67588fd1d716514cb1669e1'}

def verify():
    root=tool.ROOT/BASE/'private-evidence'
    for name,pin in PINS.items():
        assert sha(root/name)==pin
        files=tool.load(BASE+'private-evidence/'+name)
        assert len(files)==(51 if name.startswith('original') else 80)
        private_files(root,files)
    a=json.loads(run(BASE,sys.executable,'verify-http.py','--native-date'))
    assert a['passed'] and a['assertions']==84 and not a['failures']
    red=subprocess.run([sys.executable,'verify-http.py'],cwd=tool.ROOT/BASE,capture_output=True,text=True)
    assert red.returncode==2 and not red.stderr
    a=json.loads(red.stdout)
    assert a['assertions']==84 and a['failures']==['C:'+n+':full body/types/order/source' for n in ['setLists','timeLists','setInfo','timeInfo']]
    a=json.loads(run(BASE,sys.executable,'verify-http.py','--suffix=-retest'))
    assert a['passed'] and a['assertions']==88 and not a['failures']
    run(BASE,sys.executable,'-m','unittest','discover','-p','test_oracle.py')
    count=queries=0
    for lane in ['B','C']:
        for suffix in ['', '-retest']:
            file=root/(lane+'-http'+suffix)/('private-retest-wire.jsonl' if suffix else 'private-wire.jsonl')
            records=[json.loads(line) for line in file.read_text().splitlines()]
            count+=len(records);queries+=sum(len(r['prepared_SQL']) for r in records)
        clean=tool.load(BASE+'private-evidence/'+lane+'-cleanup.json')
        assert clean['port_closed'] and clean['owned_runtime_removed'] and clean['auth_cache_config_source_removed'] and clean['original_service_alive']
        assert clean['retained_fixture_rows']==(0 if lane=='B' else 2)
        prep=tool.load(BASE+'private-evidence/'+lane+'-http/private-prepare.json')
        assert prep['passed'] and prep['HTTP']==prep['DDL']==0 and prep['DML']==(0 if lane=='B' else 2)
    assert (count,queries)==(18,206)
    for relative,digest in {
        'application/p/logic/Nutrition.php':'824b19d4ca6a3d3eced4c228edb88c4bdab081477897472b3a753b01bf12765f',
        'application/p/logic/NutritionTime.php':'40da145b0fff9daa733872e6ffb3eeb9958de55ca66f6d87336efce8d2461c04',
    }.items():assert sha(tool.ROOT.parent/'zhctproject/store'/relative)==digest

def targets():
    paths=['/p/nutrition_set/lists','/p/nutrition_set/listsAllSet','/p/nutrition_time/lists',
           '/p/nutrition_set/info','/p/nutrition_time/info']
    return [item('store','POST',path,BASE,register_new_runtime_key=True,runtime_observed_method='POST',
        runtime_inventory_scope='controller-direct-http-observed',runtime_direct_http_proof=BASE+'REPORT.md',
        runtime_client_source='PROPOSAL.md exact current UI references; info direct-controller only',
        runtime_controller_source='application/p/controller/'+('NutritionTime.php' if 'nutrition_time' in path else 'NutritionSet.php')+' implicit POST observed; filename/source see REPORT',
        parity='partial-independent-per-snapshot-full-native-types-values-order-not-cross-snapshot-full-equality',
        boundary_empty_min_max='partial-first-page-positive-natural-or-two-owned-dictionary-rows-other-branches-unverified')
        for path in paths]

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