<?php

namespace App\Service\Events;

use App\Repository\MembersRepository;
use App\Repository\SubRepository;
use App\Service\Mailer\Event\CreatedAdminEvent;
use App\Service\Mailer\Mailer;
use Symfony\Component\EventDispatcher\Attribute\AsEventListener;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;

#[AsEventListener(event: AdvertEvent::class, method: 'onAdvertEvent')]
class EventSubscriber
{
    public function __construct(
        private readonly UrlGeneratorInterface $urlGenerator,
        private readonly SubRepository $subRepository,
        private readonly MembersRepository $membersRepository,
        private readonly Mailer $mailer
    ) {
    }

    public function onAdvertEvent(AdvertEvent $advertEvent): void
    {
        $event = $advertEvent->getEvent();
        foreach ($this->membersRepository->findAll() as $member) {
            $this->mailer->send($member->getEmail(),$member->getPseudo(),"[E-Cosplay] - Nouveaux événement","mails/event/new.twig",[
                'event' => $event,
                'url' => $this->urlGenerator->generate('app_event_details',['id'=>$event->getId()],UrlGeneratorInterface::ABSOLUTE_URL),
            ]);
        }

    }
}
