<?php
declare(strict_types=1);
namespace PpPhp\Core\Platform\Event\Streaming\Replay\Validation;
use PpPhp\Core\Platform\Event\Streaming\MessageBus;
use PpPhp\Core\Platform\Event\Streaming\Publisher\PublishRequest;
use PpPhp\Core\Platform\Event\Streaming\Publisher\PublisherManager;
use PpPhp\Core\Platform\Event\Streaming\Replay\StreamPersistenceRuntime;
use PpPhp\Core\Platform\Event\Streaming\Replay\StreamReplayRequest;
final class StreamPersistenceVerifier
{
    public function verify(string $basePath): array { MessageBus::resetInstance($basePath)->clear(); PublisherManager::resetInstance($basePath)->clear(); $publisher=PublisherManager::instance($basePath); for($i=1;$i<=5;$i++){$publisher->publish(PublishRequest::create('replay.publisher','replay.topic','replay.event',['i'=>$i]));} for($i=1;$i<=2;$i++){$publisher->publish(PublishRequest::create('replay.publisher','replay.other','replay.event',['i'=>$i]));} $runtime=StreamPersistenceRuntime::resetInstance($basePath); $runtime->clear(); $persisted=$runtime->persistBus(); $allReplay=$runtime->replay(StreamReplayRequest::create('',1,10,'verifier')); $topicReplay=$runtime->replay(StreamReplayRequest::create('replay.topic',1,10,'verifier')); $offsetReplay=$runtime->replay(StreamReplayRequest::create('',3,10,'verifier')); $runtime->save(); $second=StreamPersistenceRuntime::resetInstance($basePath); $second->hydrate(); $summary=$second->summary(); $stats=$summary['statistics']; $checks=['persist'=>$persisted===7,'replayAll'=>$allReplay->replayed()===7,'replayTopic'=>$topicReplay->replayed()===5,'replayOffset'=>$offsetReplay->replayed()===5,'statistics'=>($stats['events']??0)===7&&($stats['replayed']??0)===17,'persistence'=>$second->store()->exists(),'hydration'=>count($summary['events'])===7]; $issues=[]; foreach($checks as $check=>$ok){ if(!$ok) $issues[]=['check'=>$check,'message'=>'Stream persistence verification failed: '.$check]; } return ['ok'=>!in_array(false,$checks,true),'checks'=>$checks,'issues'=>$issues,'summary'=>['events'=>$stats['events']??0,'replays'=>$stats['replays']??0,'replayed'=>$stats['replayed']??0,'score'=>in_array(false,$checks,true)?80:100]]; }
}
