<?php
declare(strict_types=1);

namespace PpPhp\Core\Platform\Event\Lifecycle;

final class LifecycleEventRegistry
{
    private array $events = [];

    public function register(string $className, string $name, string $category, string $phase, string $subject): void
    {
        $this->events[$name] = [
            'class' => $className,
            'name' => $name,
            'category' => $category,
            'phase' => $phase,
            'subject' => $subject,
        ];
    }

    public function get(string $name): ?array { return $this->events[$name] ?? null; }
    public function all(): array { return array_values($this->events); }

    public function byCategory(string $category): array
    {
        return array_values(array_filter($this->events, static fn (array $event): bool => $event['category'] === $category));
    }

    public function categories(): array
    {
        return array_values(array_unique(array_map(static fn (array $event): string => $event['category'], $this->events)));
    }

    public function count(): int { return count($this->events); }

    public function toArray(): array
    {
        return ['count' => $this->count(), 'categories' => $this->categories(), 'events' => $this->all()];
    }
}
