#!/usr/bin/env python3
"""Root-only integration of the single repaired Store User.feedback path."""
import hashlib
import json
from pathlib import Path
import subprocess
import sys

import integrate_20261010 as tool
from integrate_20261011_batches46_48 import item

PRIVATE = Path('/Users/wiscourper/Documents/workspace/local/dm8-store-feedback-f1-actual-20261011.kar0Rh')
STORE_REPO = tool.ROOT.parent / 'zhctproject/store'
EVIDENCE = 'work_store/apifox/2026-09-30-dm8-interface-acceptance/parallel-store-feedback-f1-20261011/ROOT_REVIEW_FEEDBACK_F1_20261011.md'


def verify():
    assert (tool.ROOT / EVIDENCE).is_file()
    manifest_bytes = (PRIVATE / 'manifest.json').read_bytes()
    manifest = json.loads(manifest_bytes)
    assert hashlib.sha256(manifest_bytes).hexdigest() == '47a829d790b4cc0b0e41ae5735a24c5a8c4ea8c5822ec77172a64f35f87c182b'
    assert manifest['status'] == 'F1_SYNTHETIC_FEEDBACK_BOUNDED_PASS'
    assert manifest['source_map_SHA'] == '579f33e733a7277bcfbca61d7ab458f5ec14b05ec3944c1480792579804fe872'
    assert manifest['plan_SHA'] == '6055e3f934037d072f65256fde4ff7a6fd1c66ab416454b5a674b0c966cb04bb'
    assert manifest['literal'] == 'POST /p/User/feedback' and manifest['matrix_normalized'] == 'POST /p/user/feedback'
    assert manifest['HTTP'] == {'B': 1, 'C': 1} and manifest['INSERT_affected'] == {'B': 1, 'C': 1}
    assert manifest['parent_lastID_once'] and manifest['parent_lastID_raw_string'] == {'B': '1', 'C': '1'}
    assert manifest['transaction_calls'] == 0 and manifest['autocommit_persisted_after_stop']
    assert manifest['fullrow_columns'] == 7 and manifest['creator_UTF8_le32'] and manifest['actor_config_same']
    assert manifest['U'] == manifest['D'] == manifest['DDL'] == manifest['other_HTTP'] == manifest['Role_SQL'] == manifest['AI_SQL'] == 0
    assert manifest['new_reads'] == {'B': 21, 'C': 28} and manifest['owner_cumulative'] == {'B': 950, 'C': 1157}
    assert manifest['resources_clean'] and manifest['old_R1_102_assets_unchanged']
    for entry in manifest['files']:
        raw = (PRIVATE / entry['path']).read_bytes()
        assert len(raw) == entry['bytes'] and hashlib.sha256(raw).hexdigest() == entry['sha256']
        assert (PRIVATE / entry['path']).stat().st_mode & 0o777 == 0o600
    for relative, expected in [
        ('application/p/controller/User.php', 'c0821b1bb491199b0ca7ca459d3d052821a5d1bcf41deea9ba731b60c1361adf'),
        ('application/p/logic/User.php', '9da6868f5cdf09306bb892e814229d9da30833d02d6efe253ad185e7be1fe06d'),
    ]:
        assert hashlib.sha256((STORE_REPO / relative).read_bytes()).hexdigest() == expected
    run = subprocess.run(['docker', 'exec', 'zhctapp_v2_0', '/usr/local/php7.3/bin/php',
        '/workspace/zhct/zhctproject/store/tests/UserFeedbackCreatorContractTest.php'],
        check=True, capture_output=True, text=True)
    assert json.loads(run.stdout)['assertions'] == 7


def targets():
    return [item('store', 'POST', '/p/user/feedback', EVIDENCE.rsplit('/ROOT_REVIEW', 1)[0] + '/',
        evidence=EVIDENCE,
        from_source='runtime-route-dev-zhct_dev',
        module='用户反馈', terminal='PC端', role='existing-authorized-marked-test-actor',
        risk='single-synthetic-user-feedback-insert',
        scope='store-primary-single-owned-synthetic-feedback-insert-only',
        request_type='application/x-www-form-urlencoded',
        success_case='partial-evidence-one-marked-feedback-create-per-engine-original-name-CREATE_BY-and-autocommit',
        mysql_state='tested-pass', dm_state='tested-pass',
        readback='tested-pass-original-seven-column-row-after-worker-stop-and-parent-lastID',
        parity='partial-evidence-original-response-identical-source-name-and-seven-column-source-readback',
        cleanup='retained-one-marked-synthetic-feedback-per-lane-worker-token-cache-source-released',
        missing_required='not-tested', invalid_type='not-tested', duplicate_retry='not-tested',
        boundary_empty_min_max='not-tested', performance='not-tested')]


if __name__ == '__main__':
    audit = Path(__file__).with_name('integration-20261011-batch95.json')
    assert not audit.exists(), 'Preserve any previous integration evidence'
    tool.main(targets(), audit, verify)
