#!/usr/bin/env python3
"""Root-only integration of one bounded AI Store app_theme insert branch."""
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-ai-setting-app-theme-exec-20261011.fbFGRc')
AI_REPO = tool.ROOT.parent / 'zhctproject/ai_api'
EVIDENCE = 'work_ai_api/2026-09-30-dm8-interface-acceptance/parallel-ai-setting-app-theme-20261011/ROOT_REVIEW_SETTING_UPDATE_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() == '70a6457f38a42074a5eef8740ac37e0487ab096ddfcc1cc1ef3df20a387179bd'
    assert manifest['scope'].startswith('A3 exact own app_theme once-only INSERT B/C')
    result = subprocess.run([sys.executable, '-B', str(PRIVATE / 'verify.py')],
        check=True, capture_output=True, text=True)
    verification = json.loads(result.stdout)
    assert verification['pass'] and verification['assets'] == 148
    assert verification['audit']['pass'] and verification['audit']['checks'] == 12802
    for lane, old_owner, owner, reads, native, sql in [
        ('B', 974, 996, 20, 9, 22), ('C', 1192, 1219, 26, 16, 27)]:
        s = verification['audit']['summary'][lane]
        assert s['HTTP'] == s['INSERT'] == s['affected'] == s['commit'] == s['row_retained'] == s['full_physical_row'] == s['wire'] == s['cleanup'] == 1
        assert s['lastID'] == '0 string'
        assert s['SQL_ledger'] == sql and s['executed_readonly'] == reads and s['native_total'] == native
        assert s['old_owner'] == old_owner and s['owner_cumulative'] == owner
    assert verification['DB'] == verification['HTTP'] == 0
    assert subprocess.run(['git', '-C', str(AI_REPO), 'rev-parse', 'HEAD'],
        check=True, capture_output=True, text=True).stdout.strip().startswith('72cf948d')


def targets():
    return [item('ai_api', 'POST', '/store/Setting/update', EVIDENCE.rsplit('/ROOT_REVIEW', 1)[0] + '/',
        evidence=EVIDENCE,
        from_source='controller-action-source',
        terminal='AI-Store-merchant-PC', role='existing-authorized-marked-merchant-operator',
        risk='single-own-tenant-theme-setting-insert',
        scope='AI-primary-exact-own-store2147001111-key-app_theme-absence-insert-only',
        request_type='application/json-original-Theme.vue-record-types',
        success_case='partial-evidence-one-own-app_theme-insert-on-absent-key-per-engine',
        mysql_state='tested-pass', dm_state='tested-pass',
        readback='tested-pass-full-own-six-column-setting-row-and-original-theme-JSON-after-stop',
        parity='partial-evidence-identical-request-response-and-exact-theme-value-types-per-lane-no-global-setting-snapshot',
        cleanup='retained-own-app_theme-row-original-store-user-unchanged-private-cache-worker-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-batch98.json')
    assert not audit.exists(), 'Preserve any previous integration evidence'
    tool.main(targets(), audit, verify)
