<?php
declare(strict_types=1);
$runtime=$argv[1]??'';
if(!preg_match('#^/tmp/cdx-device-catalog-C-[A-Za-z0-9]+/private-runtime$#D',$runtime)||is_file($runtime.'/raw-CARD_SOURCE-private.json')){throw new RuntimeException('Pre-HTTP tool-repair guard');}
$file=$runtime.'/card-collection-private.json';$raw=file_get_contents($file);$collection=json_decode($raw,true,512,JSON_THROW_ON_ERROR);
if(count($collection['item'])!==1||($collection['item'][0]['name']??'')!=='CARD_SOURCE'||isset($collection['item'][0]['item'])){throw new RuntimeException('Unexpected collection shape');}
$collection['item']=[['name'=>'Reviewed supplemental card read','item'=>$collection['item']]];
file_put_contents($file,json_encode($collection,JSON_UNESCAPED_UNICODE|JSON_PRETTY_PRINT|JSON_THROW_ON_ERROR));chmod($file,0600);
$proof=['tool_error'=>'native CLI requires collection folder envelope','old_sha256'=>hash('sha256',$raw),'new_sha256'=>hash_file('sha256',$file),'http_requests_before_repair'=>0,'db_dml_ddl'=>0];
file_put_contents($runtime.'/card-collection-tool-repair-private.json',json_encode($proof));chmod($runtime.'/card-collection-tool-repair-private.json',0600);echo json_encode($proof),PHP_EOL;
