<?php

declare(strict_types=1);

/*
 * This file is part of the Ferienpass package.
 *
 * (c) Richard Henkenjohann <richard@ferienpass.online>
 *
 * For more information visit the project website <https://ferienpass.online>
 * or the documentation under <https://docs.ferienpass.online>.
 */

namespace Ferienpass\CoreBundle\NotificationHandler;

use Ferienpass\CoreBundle\Application\AttendanceLinkGenerator;
use Ferienpass\CoreBundle\Entity\Attendance;
use Ferienpass\CoreBundle\Message\AttendanceCompletionRequested as AttendanceCompletionRequestedEvent;
use Ferienpass\CoreBundle\Notification\AttendanceCompletionNotification;
use Ferienpass\CoreBundle\Notifier\Notifier;
use Ferienpass\CoreBundle\Repository\AttendanceRepository;
use Symfony\Component\Messenger\Attribute\AsMessageHandler;
use Symfony\Component\Notifier\Recipient\Recipient;
use Symfony\Component\Uid\Uuid;
use Symfony\Contracts\Translation\TranslatorInterface;

/**
 * Sends the e-mail whose signed link lets the participant finish a registration
 * the team created for them — sign the outstanding consents on the confirmation
 * page ({@see \Ferienpass\CmsBundle\Controller\Page\AttendanceShowController}).
 * Mirrors {@see AnonymousAttendanceUnconfirmed}.
 */
#[AsMessageHandler(bus: 'event.bus')]
class AttendanceCompletionRequested
{
    public function __construct(private readonly Notifier $notifier, private readonly AttendanceRepository $attendances, private readonly AttendanceLinkGenerator $attendanceLinks, private readonly AttendanceCompletionNotification $defaultNotification, private readonly TranslatorInterface $translator)
    {
    }

    public function __invoke(AttendanceCompletionRequestedEvent $event, Uuid $uuid): void
    {
        $attendance = $this->attendances->findOneBy(['uuid' => $event->getAttendance()]);
        if (!$attendance instanceof Attendance || $attendance->isWithdrawn()) {
            return;
        }

        $email = (string) $attendance->getParticipant()?->getEmail();
        if ('' === $email) {
            return;
        }

        // Prefer a DB-configured notification; otherwise fall back to the built-in
        // default content so the link always sends (like the e-mail-confirmation mail).
        $notification = $this->notifier->attendanceCompletion($attendance);
        if (!$notification instanceof AttendanceCompletionNotification) {
            $notification = (clone $this->defaultNotification)
                ->attendance($attendance)
                ->subject($this->translator->trans('defaults.attendance_completion.emailSubject', [], 'notifications'))
                ->content($this->translator->trans('defaults.attendance_completion.emailText', [], 'notifications'))
            ;
        }

        // The URL is signed, so — as for the e-mail confirmation link — it is built
        // from the canonical base host (router default_uri / current trusted-hosts
        // filtered request), never from an attacker-controllable Host header.
        $actionUrl = $this->attendanceLinks->signedShowUrl($attendance);

        $this->notifier->send(
            $notification->belongsTo($uuid)->actionUrl($actionUrl),
            new Recipient($email)
        );
    }
}
