<?php

declare(strict_types=1);

namespace App\Framework\EventSourcing\Replay;

use App\Framework\EventSourcing\AggregateEnvelope;

/**
 * From Version Replay Strategy
 *
 * Replays events from a specific version onwards
 */
final readonly class FromVersionReplayStrategy implements ReplayStrategy
{
    public function __construct(
        private int $fromVersion
    ) {
        if ($fromVersion < 0) {
            throw new \InvalidArgumentException('From version must be >= 0');
        }
    }

    public function selectEvents(array $events): array
    {
        return array_filter(
            $events,
            fn (AggregateEnvelope $envelope) => $envelope->version >= $this->fromVersion
        );
    }

    public function getDescription(): string
    {
        return "Replay from version {$this->fromVersion} onwards";
    }
}
