#!/usr/bin/env python3
"""Owned FeedingHistory CRUD; actual writes, API delete and original-source evidence."""
import hashlib
import json
from pathlib import Path
import subprocess
import sys
import integrate_20261010 as tool

BASE=tool.STORE+'parallel-store-feeding-history-crud-20261010/'

def verify():
    r=subprocess.run([sys.executable,str(tool.ROOT/BASE/'compare.py')],check=True,capture_output=True,text=True)
    r=json.loads(r.stdout)
    assert r['passed'] and r['check_count']==27 and all(r['checks'].values())
    m=tool.load(BASE+'manifest.json')
    assert m['unique_operations']==5 and m['actual_HTTP']['total']==49 and m['all_CLI_assertions']==138
    assert m['final_read_green_cleanup']['HTTP']==12 and m['final_read_green_cleanup']['assertions']==34
    assert m['not_run_frozen_operations']==m['own_rows_retained']==m['financial_DML']==m['DDL']==0
    assert hashlib.sha256((tool.ROOT.parent/'zhctproject/store/application/p/logic/FeedingHistory.php').read_bytes()).hexdigest()==m['candidate_logic_SHA256']
    r=tool.load(BASE+'resource-cleanup.json')
    assert r['business_token_raw_Filecache_CLI_reports_private_variables_copied_configs_and_source_tar_removed']
    assert r['unrelated_natural_rows_deleted_or_rolled_back']==0 and r['consumed_relations_final_unchanged']
    for lane in ['B','C']:
        assert r[lane]['worker_absent'] and r[lane]['port_closed'] and r[lane]['own_rows']==0
    assert r['SSH_master']['closed'] is False  # Authorized task-level ownership transfer.

def specs():
    return [dict(repo='store',method='POST',path='/p/feedingHistory/'+action,
        module='电子档案输送历史',terminal='PC-current-client-source',role='normal-type0-private-actor-owned-staff',
        risk='owned-single-table-CRUD',scope='store-primary-owned-feeding-history-and-consumed-staff-user-actor-projection',
        evidence=BASE+'REPORT.md',request_type='current-PC-form',
        parity='partial-evidence-complete-nine-column-info-five-column-list-source-readback-and-B-wire-unchanged',
        cleanup='formal-API-deleted-two-owned-records-zero-source-table-metadata-relations-unchanged-private-resources-released-master-transferred',
        boundary_empty_min_max='partial-evidence-quoted-Chinese-newline-required-invalid-date-long-text-duplicate-update-repeat-delete',
        duplicate_retry='partial-evidence-repeat-update-and-formal-delete-readback')
        for action in ['add','lists','info','update','delete']]

if __name__=='__main__':
    audit=Path(__file__).with_name('integration-20261010-batch36.json')
    assert not audit.exists(),'Preserve original transition audit'
    tool.main(specs(),audit,verify)
