#!/usr/bin/env python3
"""Original Activity chain: keep native gaps, real response reds and budget overrun."""
import hashlib
import json
from pathlib import Path
import stat
import subprocess
import sys
import integrate_20261010 as tool
from integrate_20261011_batches46_48 import item

BASE=tool.STORE+'parallel-store-activity-crud4-20261011/'

def verify():
    root=tool.ROOT/BASE;private=root/'private'
    manifest=private/'manifest.json'
    assert hashlib.sha256(manifest.read_bytes()).hexdigest()=='ff05c34aa2b924972ed09d93af8f1d655b8d14e8358a3a628f8bcbb8ec5908da'
    records=json.loads(manifest.read_text())['files'];assert len(records)==173
    for record in records:
        rel=Path(record['path']);assert not rel.is_absolute() and '..' not in rel.parts
        path=private/rel;assert not path.is_symlink() and path.is_file()
        raw=path.read_bytes()
        assert len(raw)==record['bytes'] and hashlib.sha256(raw).hexdigest()==record['sha256']
        assert oct(stat.S_IMODE(path.stat().st_mode))==record['mode']
    r=subprocess.run([sys.executable,'-B',str(root/'verify.py')],capture_output=True,text=True)
    assert r.returncode==0,r.stderr
    result=json.loads(r.stdout);assert result['checks']==409 and result['type_mutations_rejected']==7
    for lane in ['B','C']:
        assert stat.S_IMODE((private/lane).stat().st_mode)==0o700
        assert result['lanes'][lane]['HTTP']==11 and result['lanes'][lane]['captured_INSERT']==0
        assert result['lanes'][lane]['captured_UPDATE_attempts']==3 and result['lanes'][lane]['captured_DELETE']==1
        assert result['lanes'][lane]['full_final_state_exact']
        receipt=json.loads((private/lane/'resource-proof.json').read_text())
        assert receipt['source_files']==959 and receipt['source_mapping_sha256']=='790da44f00e576b9630d450dab6b4c00851fe3fec55eba817c849154be84831f'
        assert receipt['actual_non_tool_include_records_checked']==(844 if lane=='B' else 843)
    assert result['lanes']['B']['original_repeatable_code']==1 and result['lanes']['C']['original_repeatable_code']==0
    assert result['lanes']['C']['known_readonly_total']==682
    assert len(result['cross_DTO_datetime_differences'])==20
    pin=tool.load('work/2026-09-28-smart-canteen-dm8-poc/matrix-integration/STORE_APPLICATION_20261011_0955.json')
    assert len(pin)==959
    for relative,digest in pin.items():
        assert hashlib.sha256((tool.ROOT.parent/'zhctproject/store/application'/relative).read_bytes()).hexdigest()==digest

def targets():
    result=[item('store','POST','/p/activity/add',BASE,
        risk='owned-future-activity-actual-row-created-original-native-INSERT-not-captured',
        mysql_state='partial-evidence-created-row-and-HTTP-native-INSERT-missing',
        dm_state='partial-evidence-created-row-and-HTTP-native-INSERT-missing',
        success_case='partial-evidence-first-INSERT-source-row-effect-only-not-complete-native-write-proof',
        parity='partial-complete-success-envelope-original-native-binds-affected-missing'),
        item('store','POST','/p/activity/update',BASE,risk='owned-future-activity-exact-native-update-fields-and-whole-row-readback'),
        item('store','POST','/p/activity/delete',BASE,risk='only-new-own-UUID-PK-tenant-and-registration-zero-lease-delete-whole-baseline-restored'),
        item('store','POST','/p/activity/able',BASE,
             risk='owned-canonical-status-original-repeated-update-rowcount-difference',
             dm_state='tested-fail-repeatable-native-matched-row-success-vs-source-nochange-failure',
             parity='tested-fail-repeatable-B-code1-C-code0-whole-row-nochange',
             duplicate_retry='tested-fail-original-repeat-hide-B0-C1-affected-count')]
    for path in ['/p/activity/info','/p/activity/lists']:
        result.append(item('store','POST',path,BASE,
            risk='readonly-own-nonempty-complete-activity-DTO-four-business-DATETIME-fields-differ',
            dm_state='tested-fail-activity-DATETIME-zero-fraction-response-difference',
            parity='tested-fail-four-business-time-strings-seconds-vs-dot000000',
            boundary_empty_min_max='partial-owned-future-nonempty-and-final-empty-no-broad-date-input-claim'))
    return result

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