<?php

namespace App\Event\Seo;

use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpKernel\Event\ResponseEvent;
use Symfony\Component\HttpKernel\KernelEvents;

class SeoBlockerSubscriber implements EventSubscriberInterface
{
    private string $projectDir;

    public function __construct(string $projectDir)
    {
        $this->projectDir = $projectDir;
    }

    public static function getSubscribedEvents(): array
    {
        return [
            KernelEvents::RESPONSE => 'onKernelResponse',
        ];
    }

    public function onKernelResponse(ResponseEvent $event): void
    {
        $request = $event->getRequest();
        $path = $request->getPathInfo();

        $blockedPaths = ['/intranet', '/signature', '/paiment', '/payment', '/crm', '/etl'];
        foreach ($blockedPaths as $blockedPath) {
            if (str_starts_with($path, $blockedPath)) {
                $event->getResponse()->headers->set('X-Robots-Tag', 'noindex, nofollow');
                return;
            }
        }

        // Check if var/.online exists
        if (!file_exists($this->projectDir . '/var/.online')) {
            $event->getResponse()->headers->set('X-Robots-Tag', 'noindex, nofollow');
        }
    }
}
