<?php
declare(strict_types=1);

namespace PpPhp\Core\Platform\Event\Retry\DeadLetter;

final class DeadLetterQueue
{
    private array $failed = [];

    public function add(FailedEvent $event): void { $this->failed[$event->id()] = $event; }
    public function remove(string $id): bool
    {
        if (!isset($this->failed[$id])) { return false; }
        unset($this->failed[$id]);
        return true;
    }

    public function get(string $id): ?FailedEvent { return $this->failed[$id] ?? null; }
    public function all(): array { return array_values($this->failed); }
    public function clear(): void { $this->failed = []; }
    public function count(): int { return count($this->failed); }

    public function toArray(): array
    {
        return array_map(static fn (FailedEvent $event): array => $event->toArray(), $this->all());
    }
}
