#!/usr/bin/env python3
"""Four new terminal Goods write keys; existing basic GET evidence enhanced."""
import importlib.util
from pathlib import Path
import sys
import integrate_20261010 as tool
from integrate_20261011_batches40_42 import manifest, private_files, run, sha
from integrate_20261011_batches46_48 import item

BASE = tool.AI+'parallel-ai-terminal-goods-write4-20261011/'

def verify():
    m = tool.load(BASE+'manifest.json')
    manifest(BASE, m['files'])
    root = tool.ROOT/BASE
    sys.path.insert(0, str(root))
    try:
        modules = {}
        for name in ['compare', 'sql_audit']:
            spec = importlib.util.spec_from_file_location(name, root/(name+'.py'))
            module = importlib.util.module_from_spec(spec);sys.modules[name] = module
            spec.loader.exec_module(module);modules[name] = module
        r = modules['compare'].verify()
        assert r['HTTP'] == r['positive_source_and_effect_checks'] == 16
        assert len(r['complete_BC_body_pairs']) == 8 and r['business_reds'] == 0
        s = modules['sql_audit'].audit()
        assert s == m['sql']
        for lane in ['B', 'C']:
            c = s[lane]['counts']
            assert c['HTTP'] == 8 and c['native_assertions'] == 32
            assert c['API_INSERT'] == 5 and c['API_UPDATE'] == 4
            assert c['API_DELETE_statements'] == 3 and c['API_deleted_original_rows'] == 2
            assert c['DDL'] == c['preparation_DML'] == c['read_DML'] == 0
            assert s[lane]['physical_deleted_rows'] == dict(goods_sku=1, goods_image=1, goods_spec_rel=0)
            assert 'NOT PDOStatement.queryString' in s[lane]['SQL_evidence_layer']
    finally:
        sys.path.pop(0)
        for name in ['compare', 'sql_audit']:sys.modules.pop(name, None)
    run(BASE, sys.executable, '-m', 'unittest', 'discover', '-p', 'test_compare.py')
    for lane in ['B', 'C']:
        private_files(root/'private-evidence'/lane, m['raw'][lane])
        c = m['cleanup'][lane]
        assert c['port_closed'] and c['pid_gone'] and c['source_removed'] and c['private_cache_cli_header_native_removed']
        assert c['cache_files_before_cleanup'] == 0 and c['evidence_0600'] and c['evidence_dir_0700'] and c['preserved_master_original18102']
    for relative, digest in m['actual_source_sha256'].items():
        assert sha(tool.ROOT.parent/'zhctproject/ai_api'/relative) == digest, relative
    assert m['interfaces'] == 4 and m['native_assertions'] == 64 and m['business_reds'] == 0
    manifest(BASE, m['files'])

def targets():
    result = [item('ai_api', 'POST', '/terminal/Goods/'+action, BASE,
        risk='isolated-owned-catalog-CRUD-no-real-sale-or-funds',
        boundary_empty_min_max='partial-evidence-zero-stock-single-SKU-new-marker-add-edit-price-weight-up-down-softdelete-and-source-replacement')
        for action in ['add', 'edit', 'state', 'delete']]
    result.append(item('ai_api', 'GET', '/terminal/Goods/basic', BASE, evidence_only=True))
    return result

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