#!/usr/bin/env python3
"""Root reviewed timestamp schema + real owned Material workflow; no replay."""
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='work/2026-09-28-smart-canteen-dm8-poc/material-on-update-schema-20261011/'

def verify():
    groups={
        'private-evidence':(10,'19915a8bd5080bbbc11640aeb2fad5231a0aedc92cf7dae6f03f3632c8b06238'),
        'read-private-evidence':(4,'b199265c41012f86cf0b16f94aef3bc76a564fd32c7821b8df7476ba6b64b5dc'),
        'schema-private-evidence':(8,'ae532acc8bac884870566a08311b92c5366cbd64954e26b8caa4714f459dfdcd'),
        'api-private-evidence':(92,'bf4f94f9470bcc5b8cffd95eef6eb3b9745f98b564b98ce4b61817f318dbc184'),
        'cleanup-private-evidence':(5,'d1429a0356d0894aa3ffd7718396f74e12d3b08bc6d4e22e0182557add91c155'),
    }
    for group,(size,pin) in groups.items():
        root=tool.ROOT/BASE/group
        assert sha(root/'manifest.json')==pin
        records=tool.load(BASE+group+'/manifest.json')['files']
        assert len(records)==size
        files={r['path']:r for r in records}
        private_files(root,files)
        for name,record in files.items():assert (root/name).stat().st_size==record['bytes']
    schema=json.loads(run(BASE,sys.executable,'verify-schema.py'))
    assert schema['passed'] and schema['DDL_actual_success']==2 and schema['DML']==schema['HTTP']==0
    assert schema['approved_5_old_rows_time_mapping'] and schema['other_10_columns_exact'] and schema['indexes_constraints_exact']
    assert schema['trigger_enabled_proven_from_current_view_definition'] and schema['trigger_object_status']=='VALID'
    assert schema['trigger_definition_matches_approved'] and schema['CLI_time_bytes']==19
    actual=json.loads(run(BASE,sys.executable,'verify-api.py'))
    assert actual['passed'] and (actual['HTTP'],actual['DML'],actual['DDL'])==(12,32,0)
    assert actual['noop_native_rowCount']=={'B':0,'C':1}
    full=json.loads(run(BASE,sys.executable,'verify-api-contract.py'))
    assert full==dict(HTTP=0,DB=0,full_response_and_own_effect_checks=12,complete_BC_body_pairs=6,
        explicit_time_all_other_fields_preserved_checks=4)
    run(BASE,sys.executable,'-m','unittest','discover','-p','test_api_contract.py')
    clean=json.loads(run(BASE,sys.executable,'verify-cleanup.py'))
    assert clean['passed'] and clean['DB_queries']==0 and clean['raw_preserved'] and clean['protected_original_services_alive']
    assert clean['local_source_archive_removed']
    assert sha(tool.ROOT.parent/'zhctproject/store/application/p/logic/MaterialCategory.php')=='29ccbcfad2e1cfcb77a0bdbb339d40bec8b30889a4c5b5aad00babbe7671d139'

def targets():
    return [item('store','POST','/p/MaterialCategory/'+action,BASE,
        evidence=BASE+'ROOT_ACCEPTANCE.md',
        **({'from_repo':'unreviewed','from_source':'apifox-2026-05-08'} if action=='del' else {'evidence_only':True}),
        risk='owned-material-parent-leaf-CRUD-plus-approved-target-schema',
        boundary_empty_min_max='partial-real-default-parent-move-read-softdelete-noop-explicit-time-self-assignment-not-full-invalid-or-concurrency')
        for action in ['add','update','read','del']]

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