<?php

declare(strict_types=1);

/*
 * Chill is a software for social workers
 *
 * For the full copyright and license information, please view
 * the LICENSE file that was distributed with this source code.
 */

namespace Chill\MainBundle\Notification\Email\NotificationEmailHandlers;

use Chill\MainBundle\Notification\Email\NotificationEmailMessages\SendImmediateNotificationEmailMessage;
use Chill\MainBundle\Notification\Email\NotificationMailer;
use Chill\MainBundle\Repository\NotificationRepository;
use Chill\MainBundle\Repository\UserRepository;
use Psr\Log\LoggerInterface;
use Symfony\Component\Mailer\Exception\TransportExceptionInterface;
use Symfony\Component\Messenger\Attribute\AsMessageHandler;

#[AsMessageHandler]
readonly class SendImmediateNotificationEmailHandler
{
    public function __construct(
        private NotificationRepository $notificationRepository,
        private UserRepository $userRepository,
        private NotificationMailer $notificationMailer,
        private LoggerInterface $logger,
    ) {}

    /**
     * @throws TransportExceptionInterface
     * @throws \Exception
     */
    public function __invoke(SendImmediateNotificationEmailMessage $message): void
    {
        $notification = $this->notificationRepository->find($message->getNotificationId());
        $addressee = $this->userRepository->find($message->getAddresseeId());

        if (null === $notification) {
            $this->logger->error('[SendImmediateNotificationEmailHandler] Notification not found', [
                'notification_id' => $message->getNotificationId(),
            ]);

            throw new \InvalidArgumentException(sprintf('Notification with ID %s not found', $message->getNotificationId()));
        }

        if (null === $addressee) {
            $this->logger->error('[SendImmediateNotificationEmailHandler] Addressee not found', [
                'addressee_id' => $message->getAddresseeId(),
            ]);

            throw new \InvalidArgumentException(sprintf('User with ID %s not found', $message->getAddresseeId()));
        }

        try {
            $this->notificationMailer->sendEmailToAddressee($notification, $addressee);
        } catch (\Exception $e) {
            $this->logger->error('[SendImmediateNotificationEmailHandler] Failed to send email', [
                'notification_id' => $message->getNotificationId(),
                'addressee_id' => $message->getAddresseeId(),
                'stacktrace' => $e->getTraceAsString(),
            ]);
            throw $e;
        }
    }
}
