<?php


namespace AutoAdmin\Bundle\Seo\EventListener;


use AutoAdmin\Bundle\Content\Event\PostContentMetadataEvent;
use AutoAdmin\Bundle\Content\Frontend\FrontendUrlChecker;
use AutoAdmin\Bundle\Seo\Schema\SchemaBuilderManager;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;

class SchemaBuildSubscriber implements EventSubscriberInterface
{
    public function __construct(
        protected SchemaBuilderManager $schemaManager,
        protected FrontendUrlChecker $frontendUrlChecker,
    ) {
    }

    public static function getSubscribedEvents(): array
    {
        return [
            PostContentMetadataEvent::class => ['postContentMetadataEvent', -10],
        ];
    }

    public function postContentMetadataEvent(PostContentMetadataEvent $event): void
    {
        if (!$this->frontendUrlChecker->isFrontUrl($event->getRequest())) {
            return;
        }

        $contentContext = $event->getContentContext();
        $this->schemaManager->build($contentContext->getSeoPage()->getSchemaStack());
    }
}
