<?php
declare(strict_types=1);
date_default_timezone_set('UTC');
require dirname(__DIR__).'/vendor/autoload.php';
use Aen\CacheWorkshop\Laboratory;
$project=dirname(__DIR__);$action=$argv[1]??'help';
if(!in_array($action,['build','verify'],true)){fwrite(STDERR,"Usage: php bin/workshop.php build|verify\n");exit($action==='help'?0:2);}
try{
    $run=Laboratory::newRun($project);$report=(new Laboratory($run))->run();
    $json=json_encode($report,JSON_PRETTY_PRINT|JSON_UNESCAPED_UNICODE|JSON_UNESCAPED_SLASHES|JSON_THROW_ON_ERROR)."\n";
    file_put_contents($run.'/evidence.json',$json);
    if($action==='verify'){
        $expected=file_get_contents($project.'/public/data/recorded.json');
        if($expected!==$json)throw new RuntimeException('Real outcomes differ from published recording. Inspect runtime evidence.');
        echo "PASS 9 real scenarios; {$report['assertions']} assertions; recording matches byte for byte. Public unchanged.\n";
    }else{
        foreach(['data','source','vendor']as$dir)if(!is_dir($project.'/public/'.$dir))mkdir($project.'/public/'.$dir,0755,true);
        file_put_contents($project.'/public/data/recorded.json',$json);
        copy($project.'/src/Laboratory.php',$project.'/public/source/Laboratory.php.txt');
        copy($project.'/bin/workshop.php',$project.'/public/source/workshop.php.txt');
        echo "Built 9 real scenarios; {$report['assertions']} assertions; only project runtime used.\n";
    }
}catch(Throwable $error){fwrite(STDERR,'ERROR: '.$error->getMessage()."\n");exit(1);}
