<?php

declare(strict_types=1);

namespace App\Framework\EventSourcing\Replay;

use App\Framework\EventSourcing\AggregateEnvelope;

/**
 * Version Range Replay Strategy
 *
 * Replays events within a specific version range
 */
final readonly class VersionRangeReplayStrategy implements ReplayStrategy
{
    public function __construct(
        private int $fromVersion,
        private int $toVersion
    ) {
        if ($fromVersion < 0 || $toVersion < 0) {
            throw new \InvalidArgumentException('Versions must be >= 0');
        }

        if ($fromVersion > $toVersion) {
            throw new \InvalidArgumentException('From version must be <= to version');
        }
    }

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

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