#!/usr/bin/env python3
"""Sign workflow plus narrowly repaired datetime output, old red preserved."""
import hashlib
import json
import subprocess
from pathlib import Path
import integrate_20261010 as tool
from integrate_20261011_batches46_48 import item

HERE = Path(__file__).resolve().parent
PRIVATE = Path('/private/tmp/cdx-ai-hk-course-b.Kmdirn')
BASE = 'work_ai_api/2026-09-30-dm8-interface-acceptance/parallel-sign-owned-20261012/'


def verify():
    for name, digest in [
        ('SIGN_OWNED_FLOW_RED_MANIFEST_20261012.json', 'ed9867b66fd7a2e11297562b6c02113e425ad6960ec434fbd308ce70eca13e14'),
        ('SIGN_TIME_GETTER_RECHECK_MANIFEST_20261012.json', '3bb4c9e68986398221180e6d32187c379c8b83cb70391b48a81f8a4fb4b81ea5'),
        ('verify_sign_receipts.php', 'd5485af1dde396e1a0cc6e9d5a9bf0c519fd4fc4fc426d1c7eab103e438ba380'),
        ('verify_sign_time_getter_recheck.php', '58a4d09474c8b2f7409cb25765fba4041b15bc3e8898802cf33d68497ba2454a'),
        ('run_sign_owned_activity.php', '5017520e7b81867fff87213cf0a2376af29470101a0884d8d5a4a2bef340faa1'),
        ('recheck_sign_list_readonly.php', '66209fcb8d10baf387fbc3c848b9e8f1da1767ab88b1d64db731f234d0b31c68'),
    ]:
        assert hashlib.sha256((PRIVATE / name).read_bytes()).hexdigest() == digest
    run = lambda name: json.loads(subprocess.check_output(['/opt/homebrew/opt/php@7.4/bin/php', str(PRIVATE / name)]))
    old = run('verify_sign_receipts.php')
    assert old['status'] == 'verified_contract_failure' and old['db_or_http_calls'] == 0
    assert old['true_business_http_per_engine'] == 5 and old['list_zero_microsecond_format_mismatch_proven']
    assert all(t['business_pass'] and all(t['checks'].values()) for t in old['targets'].values())
    new = run('verify_sign_time_getter_recheck.php')
    assert new['status'] == 'verified' and new['four_real_http_gets'] == 4 and new['business_dml'] == 0
    assert new['B_full_body_except_trace_identical'] and new['C_only_expected_zero_microsecond_changes']
    assert new['original_rows_unchanged_all_four'] and new['status_three_and_two_own_signs_all_four']
    for name, digest in [
        ('Sign.php', '6a2b20cc4d4537b0f2664ff0b76211e55388ac955195971d484186597f661e37'),
        ('SignUser.php', '35c2a0794258c76897de265f448934a9049ed906ef472b78ae3fac9becc4c171'),
    ]:
        assert hashlib.sha256((tool.source_root('ai_api') / 'app/api/model' / name).read_bytes()).hexdigest() == digest


if __name__ == '__main__':
    audit = HERE / 'integration-20261012-batch143.json'
    assert not audit.exists()
    specs = []
    for action in ['add', 'receive', 'list', 'detail']:
        specs.append(item('ai_api', 'POST' if action in ['add', 'receive'] else 'GET', '/api/Sign/' + action, BASE,
            from_repo='unreviewed', from_source='apifox-2026-05-08',
            risk='own-short-lived-sign-activity-no-real-prize-fulfilment',
            readback='tested-pass-own-sign-0-to2-and-sign-user-status1-to2-to3-readback',
            success_case='partial-evidence-owned-two-address-sign-and-receive-or-nonempty-read',
            parity='partial-evidence-source-readback-and-datetime-zero-fraction-fix-MySQL-body-preserved',
            cleanup='own-activity-disabled-expired-natural-references-zero-fixtures-retained-token-zero'))
    tool.main(specs, audit, verify)
