<?php

declare(strict_types=1);

namespace KupShop\GTMBundle\EventListener;

use KupShop\GTMBundle\ServerSideGTMEvent\UserSubscribeEvent;
use KupShop\GTMBundle\ServerSideGTMEvent\UserUnsubscribeEvent;
use KupShop\GTMBundle\Utils\Handlers\NewsletterHandler;
use KupShop\UserBundle\Event\UserNewsletterEvent;
use KupShop\UserBundle\Event\UserRegisteredEvent;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Contracts\Service\Attribute\Required;

class UserEventListener implements EventSubscriberInterface
{
    #[Required]
    public NewsletterHandler $newsletterHandler;

    #[Required]
    public UserSubscribeEvent $userSubscribeEvent;

    #[Required]
    public UserUnsubscribeEvent $userUnsubscribeEvent;

    public static function getSubscribedEvents(): array
    {
        return [
            UserRegisteredEvent::class => [
                ['onUserRegistered', 200],
            ],
            UserNewsletterEvent::SUBSCRIBED => [
                ['onNewsletterSubscribed', 200],
            ],
            UserNewsletterEvent::UNSUBSCRIBED => [
                ['onNewsletterUnsubscribed', 200],
            ],
        ];
    }

    public function onUserRegistered(UserRegisteredEvent $event): void
    {
    }

    public function onNewsletterSubscribed(UserNewsletterEvent $event): void
    {
        $this->newsletterHandler->subscribe($event->getEmail());
        $this->userSubscribeEvent->start($event);
    }

    public function onNewsletterUnsubscribed(UserNewsletterEvent $event): void
    {
        $this->userUnsubscribeEvent->start($event);
    }
}
