<?php

declare(strict_types=1);

namespace App\Administration\Infrastructure\Messaging;

use App\Administration\Domain\Event\UtilisateurInvite;
use App\Administration\Domain\Model\ActivationToken\ActivationToken;
use App\Administration\Domain\Model\User\Role;
use App\Administration\Domain\Model\User\UserId;
use App\Administration\Domain\Repository\ActivationTokenRepository;
use App\Administration\Domain\Repository\UserRepository;
use App\Shared\Domain\Clock;
use App\Shared\Infrastructure\Tenant\TenantUrlBuilder;
use Symfony\Component\Mailer\MailerInterface;
use Symfony\Component\Messenger\Attribute\AsMessageHandler;
use Symfony\Component\Mime\Email;
use Twig\Environment;

/**
 * Sends an invitation email when a user is invited by an admin.
 *
 * Creates an activation token and emails the user with an activation link.
 */
#[AsMessageHandler(bus: 'event.bus')]
final readonly class SendInvitationEmailHandler
{
    public function __construct(
        private MailerInterface $mailer,
        private Environment $twig,
        private ActivationTokenRepository $tokenRepository,
        private UserRepository $userRepository,
        private TenantUrlBuilder $tenantUrlBuilder,
        private Clock $clock,
        private string $fromEmail = 'noreply@classeo.fr',
    ) {
    }

    public function __invoke(UtilisateurInvite $event): void
    {
        $user = $this->userRepository->get(UserId::fromString((string) $event->userId));

        $token = ActivationToken::generate(
            userId: (string) $event->userId,
            email: $event->email,
            tenantId: $event->tenantId,
            role: $event->role,
            schoolName: $user->schoolName,
            createdAt: $this->clock->now(),
        );

        $this->tokenRepository->save($token);

        $roleEnum = Role::tryFrom($event->role);
        $roleLabel = $roleEnum?->label() ?? $event->role;

        $activationUrl = $this->tenantUrlBuilder->build($event->tenantId, '/activate/' . $token->tokenValue);

        $html = $this->twig->render('emails/invitation.html.twig', [
            'firstName' => $event->firstName,
            'lastName' => $event->lastName,
            'role' => $roleLabel,
            'activationUrl' => $activationUrl,
        ]);

        $email = (new Email())
            ->from($this->fromEmail)
            ->to($event->email)
            ->subject('Invitation à rejoindre Classeo')
            ->html($html);

        $this->mailer->send($email);
    }
}
