#!/usr/bin/env python3
"""Equipment list read: repaired weights, unresolved old schema field contract."""
import hashlib
import json
from pathlib import Path
import integrate_20261010 as tool
from integrate_20261011_batches46_48 import item
HERE=Path(__file__).resolve().parent
BASE='work_store/apifox/2026-09-30-dm8-interface-acceptance/parallel-prepay-code-scan-20261012/'
REPORT=BASE+'EQUIPMENT_LIST_ROOT_REVIEW.md'

def pinned_files():
    m=json.loads((tool.ROOT/BASE/'READ_REGRESSION_MANIFEST.json').read_text())
    root=Path(m['private_root'])
    for spec in m['files']:
        assert hashlib.sha256((root/spec['name']).read_bytes()).hexdigest()==spec['sha256']
    return root

def verify():
    root=pinned_files();rows=[]
    for lane in ['B','C']:
        r=json.loads((root/(lane+'-equipment-list-owned-listfix-receipt.json')).read_text())
        raw=(root/(lane+'-equipment-list-owned-listfix-response-redacted.json')).read_bytes()
        assert hashlib.sha256(raw).hexdigest()==r['redacted_response_sha256']
        assert r['passed'] and r['http_status']==200 and r['response_code']==0
        assert r['original_row_unchanged'] and r['original_row_sha256_before']==r['original_row_sha256_after']
        assert r['mqtt_stub_events_delta']==1 and r['mqtt_stub_external_calls']==0
        assert r['source_sha256']['logic']=='d3040978901043c66d166820da1b9cf226e335da20baeb9c6d9c992cf6f35f57'
        body=json.loads(raw);assert body['data']['total']==1 and len(body['data']['rows'])==1
        row=body['data']['rows'][0]
        for field in ['communicationkey','matecode','sn','code']:assert row[field]=='[REDACTED]'
        for field in ['left_dishes_weight','right_dishes_weight','left_dishes_weight_max','right_dishes_weight_max']:
            assert row[field]=='0.00' and r['returned_row_types'][field]=='string'
        rows.append(row)
    assert rows[0]['receipt_template']=='qr_large' and type(rows[1]['receipt_template']) is int and rows[1]['receipt_template']==1
    b=json.loads((root/'B-decimal-equipment-metadata-receipt.json').read_text())
    c=json.loads((root/'C-decimal-equipment-metadata-receipt.json').read_text())
    bc=next(x for x in b['equipment_columns'] if x['column_name']=='receipt_template')
    cc=next(x for x in c['equipment_columns'] if x['column_name']=='RECEIPT_TEMPLATE')
    assert bc['column_type']=='varchar(32)' and bc['column_default']=='qr_large'
    assert cc['data_type']=='SMALLINT' and cc['column_default']=='1'
    assert json.loads((root/'C-equipment-list-owned-receipt.json').read_text())['http_status']==404
    assert json.loads((root/'C-equipment-list-owned-retry-receipt.json').read_text())['http_status']==200

if __name__=='__main__':
    audit=HERE/'integration-20261012-batch124.json';assert not audit.exists()
    targets=[item('store','POST','/p/equipment/lists',BASE,evidence=REPORT,
        from_repo='unreviewed',from_source='apifox-2026-05-08',
        mysql_state='tested-pass-owned-nonempty-device-list-original-row-unchanged',
        dm_state='tested-fail-receipt-template-cross-snapshot-schema-response-parity-open-weight-fix-passed',
        readback='tested-pass-own-device-readback-MQTT-hard-stub-no-external-call',
        success_case='partial-evidence-owned-type60-one-row-not-all-device-types',
        parity='tested-fail-receipt-template-varchar-versus-smallint-original-schema-difference-four-decimal-weights-fixed',
        cleanup='no-DML-original-row-unchanged-sensitive-fields-redacted-no-full-raw-retained')]
    tool.main(targets,audit,verify)
