<?php
declare(strict_types=1);
namespace PpPhp\Core\Platform\Event\Streaming\Replay;
final class StreamReplayResult
{
    public function __construct(private readonly array $request = [], private readonly array $events = [], private readonly int $replayed = 0, private readonly float $replayTimeMs = 0.0, private readonly string $createdAt = '') {}
    public static function create(StreamReplayRequest $request, array $events, float $replayTimeMs): self { return new self($request->toArray(), array_map(static fn($event): array => $event instanceof StreamPersistedEvent ? $event->toArray() : (array)$event, $events), count($events), $replayTimeMs, date('c')); }
    public static function fromArray(array $data): self { return new self((array)($data['request']??[]),(array)($data['events']??[]),(int)($data['replayed']??0),(float)($data['replayTimeMs']??0.0),(string)($data['createdAt']??'')); }
    public function replayed(): int { return $this->replayed; }
    public function toArray(): array { return ['request'=>$this->request,'events'=>$this->events,'replayed'=>$this->replayed,'replayTimeMs'=>round($this->replayTimeMs,3),'createdAt'=>$this->createdAt !== '' ? $this->createdAt : date('c')]; }
}
