#!/usr/bin/env python3
"""Register actual dictionary, metadata, and dining write/readback HTTP evidence."""
import json
from pathlib import Path
import subprocess
import sys
import integrate_20261010 as tool

DICT = tool.AI + 'parallel-ai-store-dictionary-post-20261010/'
FILES = tool.AI + 'parallel-ai-file-metadata-http-20261010/'
DINING = tool.STORE + 'parallel-store-dining-config-write-20261010/'

def verify():
    result = subprocess.run([sys.executable, str(tool.ROOT / DICT / 'compare.py'),
        str(tool.ROOT / DICT / 'evidence-B.json'), str(tool.ROOT / DICT / 'evidence-C.json')],
        check=True, capture_output=True, text=True)
    data = json.loads(result.stdout)
    assert data['status'] == 'PASS' and data['complete_wire_case_pairs'] == 4
    assert data['nonempty_post_endpoints'] == 2 and data['complete_values_types_array_order_equal']
    subprocess.run([sys.executable, str(tool.ROOT / DICT / 'test_compare.py')],
        check=True, capture_output=True)
    files = tool.load(FILES + 'results.json')
    assert files['exact_method_path_keys'] == 12
    assert files['scenario_pairs'] == files['strict_equal_pairs'] == files['wire_equal_pairs'] == 36
    assert len({(c['method'], c['path']) for c in files['cases']}) == 12
    assert all(c['source_projection_equal_both'] and c['strict_differences'] == 0
        and c['wire_shape_differences'] == 0 for c in files['cases'])
    for lane in ['B', 'C']:
        audit = files['audit'][lane]
        assert audit['natural_unchanged'] and audit['token_cleaned']
        assert audit['cleanup'] == {'files': 4, 'groups': 4}
        assert all(n == 0 for n in audit['independent_remaining'].values())
        assert audit['storage_guard_attempts'] == 0 and len(audit['list_environment_guards']) == 14
        for guard in audit['list_environment_guards']:
            assert guard['allowed'] and guard['actual_new_root'] and guard['below_2_0_7']
            assert guard['actual_version'] == '0.0.1'
            assert guard['version_sha256'] == 'c4183782e45ec544e34be623297955ef183135347f8b43c944d3579bd6a94e78'
            assert not any(guard['deletion_targets_exist'].values())
    result = subprocess.run([sys.executable, str(tool.ROOT / DINING / 'compare.py')],
        check=True, capture_output=True, text=True)
    data = json.loads(result.stdout)
    assert data['primary_write_method_keys'] == 6 and data['http_scenarios_each_engine'] == 24
    assert data['wire_and_normalized_own_value_differences'] == 0
    assert data['on_update_changed_and_noop_parity'] and data['own_rows_after_cleanup'] == 0
    assert not data['real_customer_business_or_school_branch_proven']
    for lane in tool.load(DINING + 'cleanup.json')['lanes']:
        assert lane['worker_stopped'] and lane['port_not_listening']
        assert lane['private_cache_files_remaining'] == 0 and lane['natural_source_unchanged']
        assert lane['own_area_rows_remaining'] == lane['own_shop_rows_remaining'] == 0

def targets():
    out = []
    for name in ['Nation', 'Sport']:
        out.append(dict(repo='ai_api', method='POST', path=f'/store/store.{name}/all',
            module='民族运动字典', terminal='PC端', role='isolated-merchant-cache-token', risk='read',
            scope='ai-primary-natural-complete-dictionary-readonly-no-DML', evidence=DICT + 'REPORT.md',
            parity='partial-evidence-four-complete-wire-and-source-projections-value-type-order-equal',
            request_type='actual-PC-POST-client'))
    files = tool.load(FILES + 'results.json')
    for method, path in sorted({(c['method'], c['path']) for c in files['cases']}):
        out.append(dict(repo='ai_api', method=method, path=path, module='文件元数据与分组',
            terminal='设备端' if path.startswith('/terminal/') else 'PC端',
            role='isolated-own-tenant-version-0.0.1-private-root', risk='read' if method == 'GET' else 'metadata-write',
            scope='ai-primary-own-upload-file-group-metadata-no-storage-or-delete', evidence=FILES + 'REPORT.md',
            parity='partial-evidence-36-full-wire-source-verified-generated-identities-and-clock-only-mapped',
            boundary_empty_min_max='partial-evidence-cycle-refusal-ungrouped-multiple-move-image-filter-and-cleaned-empty',
            request_type='GET-query' if method == 'GET' else 'application/json'))
    for action in ['areaCreate', 'areaUpdate', 'areaStatus', 'restaurantCreate', 'restaurantUpdate',
                   'restaurantStatus', 'areaDetail', 'restaurantDetail']:
        entry = dict(repo='store', method='POST', path='/api/dining_config/' + action,
            module='区域餐厅创建编辑状态及回读', terminal='PC端', role='isolated-disabled-admin-own-area-shop',
            risk='read' if action.endswith('Detail') else 'configuration-write',
            scope='store-primary-own-area-shop-BUSINESS_VALUE-only-no-order-device-or-school',
            evidence=DINING + 'REPORT.md', request_type='application/json',
            parity='partial-evidence-24-wire-normalized-own-values-equal-and-four-natural-read-regressions',
            duplicate_retry='partial-evidence-duplicate-create-rejected-and-noop-update-clock-preserved',
            boundary_empty_min_max='partial-evidence-Chinese-quotes-backslash-invalid-status-disabled-parent-and-required-name')
        if action != 'restaurantDetail':
            entry.update(from_repo='unreviewed', from_source='apifox-2026-05-08')
        out.append(entry)
    return out

if __name__ == '__main__':
    tool.main(targets(), Path(__file__).with_name('integration-20261010-batch6.json'), verify)
