#!/usr/bin/env python3
"""Nutrition eight write methods only; Material storage regression separate."""
import json
from pathlib import Path
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-category-crud-20261011/'
REPORT = BASE+'REPORT-NUTRITION-ONLY.md'

def verify():
    root = tool.ROOT/BASE/'private-evidence'
    assert sha(root/'manifest.json') == 'ec4620a8d38a53c912a7a7cbaa8abbfdd5ca004bccd7719d7b554e0896409a9e'
    records = tool.load(BASE+'private-evidence/manifest.json')['files']
    assert len(records) == 235
    private_files(root, {r['path']:r for r in records})
    old=json.loads(run(BASE,sys.executable,'verify-evidence.py'))
    assert (old['approved_HTTP'],old['guard_rejected_HTTP'],old['actual_DML'],old['framework_prepared']) == (30,1,36,248)
    new=json.loads(run(BASE,sys.executable,'verify-noop.py'))
    assert new['passed'] and (new['HTTP'],new['actual_DML'],new['framework_prepared']) == (16,16,124)
    whole=json.loads(run(BASE,sys.executable,'verify-nutrition-only.py'))
    assert whole == dict(HTTP_nutrition=36, complete_BC_body_pairs=16,
        own_full_row_and_effect_checks=36,original_C_noop_reds=2,
        repeated_noop_complete_source_unchanged=True,DB=0,HTTP_replayed=0)
    run(BASE,sys.executable,'-m','unittest','discover','-p','test_nutrition_oracle.py')
    expected = {
        'application/p/logic/NutritionCategory.php':'9cf92ab10eed01c7029355efebea1532aaa2e87c9d9f455087d69e61e2d8b9c4',
        'application/p/controller/NutritionCategory.php':'cdaa7cc9b13694c578053893d2955c88a9ec1e68263e909661422b472028c2bd',
        'application/m/controller/NutritionCategory.php':'5df9edf20a8c9799089ca7e95d18a6ba142180b9e8e2c298661579ffba34f7a8',
    }
    for relative,digest in expected.items():
        assert sha(tool.ROOT.parent/'zhctproject/store'/relative) == digest,relative

def targets():
    result = [item('store','POST',f'/{side}/nutritionCategory/{action}',BASE,
        evidence=REPORT,runtime_observed_method='POST',
        runtime_inventory_scope='controller-direct-http-observed',runtime_direct_http_proof=REPORT,
        runtime_controller_source=f'application/{side}/controller/NutritionCategory.php actual POST ordinary Auth, UI call unverified',
        terminal='direct-controller-HTTP-product-UI-unverified',
        risk='owned-nutrition-category-CRUD-no-financial-or-external-effects',
        cleanup='new-nutrition-zero-physical-rows-raw-retained-workers-auth-explicitly-transferred-to-Material-slice-not-stopped',
        boundary_empty_min_max='partial-owned-add-edit-status0-repeat-failure-delete-native-SQL-and-full-row-readback')
        for side in ['p','m'] for action in ['add','update','able','delete']]
    _, rows, _ = tool.read_csv(tool.COVERAGE)
    for target in result:
        matches = [r for r in rows if r['repo'] in ['store','unreviewed']
            and tool.norm(r['route_or_command']) == tool.norm(target['path']) and r['method'] in ['POST','*']]
        if not matches:
            target['register_new_runtime_key'] = True
        elif all(r['repo'] == 'unreviewed' for r in matches):
            assert len({r['source'] for r in matches}) == 1
            target.update(from_repo='unreviewed',from_source=matches[0]['source'])
    return result

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