<?php
declare(strict_types=1);

namespace PpPhp\Core\Platform\Event\Runtime;

use PpPhp\Core\Platform\Event\Attributes\EventListener;
use PpPhp\Core\Platform\Event\Discovery\AttributeListenerScanner;
use PpPhp\Core\Platform\Event\Manifest\ManifestListenerLoader;
use PpPhp\Core\Platform\Event\Registry\EventListenerRegistry;
use PpPhp\Core\Platform\Event\Subscriber\EventSubscriberInterface;
use PpPhp\Core\Platform\Event\Subscriber\SubscriberRegistrar;

final class EventRuntimeRegistry
{
    private EventListenerRegistry $registry;
    private SubscriberRegistrar $subscribers;
    private AttributeListenerScanner $attributes;
    private ManifestListenerLoader $manifests;

    public function __construct()
    {
        $this->registry = new EventListenerRegistry();
        $this->subscribers = new SubscriberRegistrar();
        $this->attributes = new AttributeListenerScanner();
        $this->manifests = new ManifestListenerLoader();
    }

    public function registry(): EventListenerRegistry { return $this->registry; }

    public function registerSubscriber(EventSubscriberInterface|string $subscriber): void
    {
        $this->subscribers->register($this->registry, $subscriber);
    }

    public function scanAttributes(array $classes): int
    {
        return $this->attributes->scanClasses($this->registry, $classes);
    }

    public function loadManifest(array $manifest, string $basePath = ''): int
    {
        return $this->manifests->load($this->registry, $manifest, $basePath);
    }

    public function summary(): array
    {
        return [
            'events' => count($this->registry->events()),
            'listeners' => $this->registry->count(),
            'subscribers' => count($this->subscribers->records()),
            'attributeWarnings' => count($this->attributes->warnings()),
            'manifestWarnings' => count($this->manifests->warnings()),
        ];
    }
}
