#!/usr/bin/env python3
"""Preserve 214 creator failure and 217 time-format failure alongside final evidence."""
import base64
import hashlib
import json
from pathlib import Path
import subprocess
import sys
import integrate_20261010 as tool
from integrate_20261011_batches46_48 import item

HERE = Path(__file__).resolve().parent
PRIVATE = Path('/Users/wiscourper/Documents/workspace/local/dm8-store-purchase-read3-20261012.L5dTYR/stage3/employment217-20261013')
BASE = 'work_store/apifox/2026-09-30-dm8-interface-acceptance/parallel-employment-20261013/'


def verify():
    for directory, hashes in [(PRIVATE, {
        'REPORT217.md': '099902155bd0e02314d01441807f117f4e8057dfe367e727ec4b79ac5448feac',
        'manifest217.json': 'e6e080f6bf7b0bf8833a32fda13e95b8751c091089a983d6eedb0be424fb0741',
        'verify217.py': '0879156a46a393969f9d6df9dc72a189c01e9380c3329ce4a1fbf6544669d2de',
    }), (PRIVATE.parent / 'employment214-20261013', {
        'REPORT214.md': '4bcf14568a55a5eb9c49d8aed1292957032908b8a30567e89ef9717d425a0be2',
        'manifest214.json': 'a1d086b9d2f7d0598cc5267a3e7689d84eac1fa7d2e4477de05220f311f17dde',
        'verify214.py': '4aa870322f03206b980bef71a862115a2e847d2a886051ba70b73abd84b1b4ed',
    })]:
        for name, digest in hashes.items():
            assert hashlib.sha256((directory / name).read_bytes()).hexdigest() == digest, name
    old = json.loads(subprocess.check_output([sys.executable, '-B', str(PRIVATE.parent / 'employment214-20261013/verify214.py')]))
    new = json.loads(subprocess.check_output([sys.executable, '-B', str(PRIVATE / 'verify217.py')]))
    assert old['checks'] == 494 and old['assets'] == 129 and old['C_true_product_length_red']
    assert new['checks'] == 572 and new['assets'] == 122 and new['final_full_DTO_cross_equal']
    for lane in ('B', 'C'):
        evidence = PRIVATE / ('evidence-' + lane)
        wire = json.loads((evidence / 'response-R217F.json').read_text())
        body = json.loads(base64.b64decode(wire['body_base64']))
        physical = json.loads((evidence / 'final-R217F.json').read_text())['own'][0]
        assert isinstance(body['data'], dict) and len(body['data']) == len(physical) == 10
        for key in physical:
            expected = physical[key]
            if lane == 'C' and key == 'create_time':
                assert expected.endswith('.000000'); expected = expected[:-7]
            assert type(body['data'][key]) is type(expected) and body['data'][key] == expected
        if lane == 'C':
            insert = json.loads((evidence / 'response-A217.json').read_text())
            assert physical['id'] == int(insert['ids']['id']) and physical['uuid'] == insert['ids']['uuid']


if __name__ == '__main__':
    audit = HERE / 'integration-20261013-batch217.json'
    assert not audit.exists(), 'Do not replay matrix integration'
    specs = [item('store', 'POST', '/p/employmentState/' + action, BASE,
        success_case='partial-evidence-original-owned-Employment-add-update-info-214B-217C-creator-and-time-red-fixed',
        readback='tested-pass-native-ten-column-rows-generated-ID-only-text-update-Staff77-precise-UINT64-unchanged',
        parity='partial-evidence-original-JSON-types-source-identities-and-time-format-MySQL-wire-unchanged',
        cleanup='own-marked-rows-retained-workers-token-cache-zero-no-Staff-or-external-write')
        for action in ('add', 'update', 'info')]
    tool.main(specs, audit, verify)
