#!/usr/bin/env python3
"""Owned cash-only supplement quote, real HTTP and original-row readback."""
import hashlib
import json
import subprocess
from pathlib import Path
import integrate_20261010 as tool
from integrate_20261011_batches46_48 import item

HERE = Path(__file__).resolve().parent
PRIVATE = tool.ROOT.parent / 'worktrees/dm8-compat-20261012/private-prepay-harness'
BASE = 'work_store/apifox/2026-09-30-dm8-interface-acceptance/parallel-supplement-quote-20261012/'


def verify():
    pins = {
        'SUPPLEMENT_QUOTE_BC_EVIDENCE_20261012.md': '07b5b0554c55799ea89d321b851aa846c77f4867b33ecbb4648e8055654666a8',
        'verify_supplement_quote_bc.php': '48a03882c6b9439d04ae5f4877479dd538e18dd7fba8a62158163f0b20f26918',
        'B-supplement-quote-http-receipt-20261012.json': 'fda139b2d35945bb7ca684a01596885b4cc3ca2b7ac9bc4e84ce7422c41f8423',
        'C-supplement-quote-http-receipt-20261012.json': '60dbe254e4546c7098e31361b817778aa8029ffe093868cb2080c94a6c730239',
        'B-supplement-quote-prepared-20261012.json': '5be5230df0cd277bd6bcf2b4a68d06cfe31f009dbfad2adcaad7d170dd14b9d0',
        'C-supplement-quote-prepared-20261012.json': '11328278a8049d22b180a9c921750e63786fda262345c32f852e73a744e110d8',
        'B-supplement-quote-cleanup-20261012.json': '18538c35494367d4f4c95705e956a23038a9d80e3d7a294fa39352060813437f',
        'C-supplement-quote-cleanup-20261012.json': '88510d960c25f57562dce4fa09a8bf606eafe17b651014b593dbaa678dc8527a',
    }
    for name, digest in pins.items():
        assert hashlib.sha256((PRIVATE / name).read_bytes()).hexdigest() == digest
    r = json.loads(subprocess.check_output(['docker', 'exec', 'zhctapp_v2_0', '/usr/local/php7.3/bin/php',
        '/workspace/zhct/worktrees/dm8-compat-20261012/private-prepay-harness/verify_supplement_quote_bc.php']))
    assert r['offline_verify'] == 'pass' and not r['database_connected'] and not r['http_replayed']
    canon = lambda v: json.dumps(v, ensure_ascii=False, separators=(',', ':'))
    quotes = []
    for lane in ['B', 'C']:
        assert r['lanes'][lane]['http_count'] == 2 and r['lanes'][lane]['source_groups'] == 11
        doc = json.loads((PRIVATE / (lane + '-supplement-quote-http-receipt-20261012.json')).read_text())
        body = json.loads((PRIVATE / (lane + '-supplement-quote-positive-response-20261012.raw')).read_text())
        assert type(body['code']) is int and body['code'] == 0
        quote = body['data']['quote']
        assert len(quote) == 19 and canon(quote) == canon(doc['responses']['positive']['quote'])
        quote.pop('quote_hash')
        quotes.append(canon(quote))
    assert quotes[0] == quotes[1]
    for name, digest in {
        'p/controller/SupplementMealOrder.php': '7943d45678ee088d20441183fdf7a504df73ef357614290312d8861ff859be33',
        'common/service/meal/SupplementOrderService.php': '8666964ccc2b8a9767b91ab89645f3b4730e18869942dce08cc2e71433723f10',
        'api/service/Consume.php': 'fe9f8b82f04cbabdb724e0cbc94de209a3199dfc5599f2e8a40d28ab0ff99d0d',
    }.items():
        assert hashlib.sha256((tool.source_root('store') / 'application' / name).read_bytes()).hexdigest() == digest


if __name__ == '__main__':
    audit = HERE / 'integration-20261012-batch147.json'
    assert not audit.exists()
    spec = item('store', 'POST', '/p/supplementMealOrder/quote', BASE,
        from_repo='unreviewed', from_source='apifox-2026-05-08',
        risk='readonly-owned-cash-only-supplement-quote-not-order-create',
        readback='tested-pass-eleven-owned-source-groups-unchanged-no-financial-ledger-writes',
        success_case='partial-evidence-cash-0.01-no-marketing-strategy-quote-only',
        invalid_type='partial-evidence-0.001-price-rejected-no-write',
        parity='partial-evidence-19-quote-fields-types-values-equal-except-independently-verified-input-hash',
        cleanup='two-owned-department-fixtures-per-engine-exactly-deleted-original-payment-fixtures-unchanged')
    tool.main([spec], audit, verify)
