#!/usr/bin/env python3
"""Verify original red plus narrowly changed real catalog HTTP before integration."""
import hashlib
import json
import subprocess
from pathlib import Path
import integrate_20261010 as tool
from integrate_20261011_batches46_48 import item
from integrate_20261012_batch148 import shape

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-ai-catalog-weight-20261012/'


def verify():
    for name, sha in [
        ('meal171-read-delete/verify_catalog_red.php', 'bf883ceb0d777defc7cf67e3d58353cc2467427d10dbb67dd18b3af685009895'),
        ('meal175-ai-catalog-weight/verify_fixed_catalog.php', 'd7b8329b05e55dd760f0dc72fcb5cfe9d7288eecaaffc09d9e8a733a046cadf6'),
        ('meal175-ai-catalog-weight/REPORT_175.md', '5fd2b776ca0cd240d849948750e3d6a14168b87010819378a06cab7d184cea2e'),
    ]:
        assert hashlib.sha256((PRIVATE / name).read_bytes()).hexdigest() == sha
    for file, status in [
        ('meal171-read-delete/verify_catalog_red.php', 'verified_171_catalog_original_type_red'),
        ('meal175-ai-catalog-weight/verify_fixed_catalog.php', 'verified_175_approved_catalog_weight_fix'),
    ]:
        r = json.loads(subprocess.check_output(['/opt/homebrew/opt/php@7.4/bin/php', str(PRIVATE / file)]))
        assert r['status'] == status
    paths = [
        ('runtime/meal171-ai-catalog-get-02ff29eebd350ef45af2836125338f6cd0ddfe7afd3d27df5ac4533dfe505d5a.json',
         'runtime/meal175-fixed-catalog-B-02ff29eebd350ef45af2836125338f6cd0ddfe7afd3d27df5ac4533dfe505d5a.json'),
        ('meal171-read-delete/C_catalog_get.json', 'meal175-ai-catalog-weight/C_fixed_get.json'),
    ]
    types = []
    for lane, pair in zip(('B', 'C'), paths):
        wires = []
        for path in pair:
            e = {x['phase']: x for x in json.loads((PRIVATE / path).read_text())['events']}
            wires.append(e['after_http']['raw_response_exact'])
        old, new = [json.loads(w) for w in wires]
        assert type(old) is list and type(new) is list and len(old) == len(new) == 10
        if lane == 'B':
            assert wires[0] == wires[1]
        else:
            for row in old:
                assert type(row['weight']) is int
                row['weight'] = format(row['weight'], '.2f')
            assert old == new and shape(old) == shape(new)
        types.append(shape(new))
    assert types[0] == types[1]
    for name, sha in [
        ('app/store/service/dishes/Dishes.php', '9fcc9ff9f4424199b71cf02146bbce1235c86be94e5982694cf6309f113c6ff2'),
        ('tests/AiDishCatalogWeightContract.php', '546281d913c61463ed7429aba7247c9a3a8f5bd2cb67a638005abceeea695df9'),
    ]:
        assert hashlib.sha256((tool.source_root('ai_api') / name).read_bytes()).hexdigest() == sha


if __name__ == '__main__':
    audit = HERE / 'integration-20261012-batch175.json'
    assert not audit.exists()
    specs = [item('ai_api', 'GET', '/api/Dishes/ai', BASE,
        risk='bounded-first-ten-natural-catalog-read-private-cache-and-Qw-hard-stub',
        readback='tested-pass-ten-original-dishes-related-file-hashes-and-DM-server-name-hex-no-DB-write',
        success_case='partial-evidence-nonempty-ten-row-catalog-source-values-per-engine',
        parity='partial-evidence-all-JSON-types-match-B-wire-unchanged-C-only-ten-weight-scale2-strings-fixed',
        cleanup='private-last_id-created-then-cleared-real-Qw-queue-zero-natural-source-unchanged')]
    tool.main(specs, audit, verify)
