<?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\CmsBundle\Controller\Fragment;

use Ferienpass\CoreBundle\Controller\AbstractPrivateFragmentController;
use Ferienpass\CoreBundle\Entity\Attendance;
use Ferienpass\CoreBundle\Repository\AttendanceRepository;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;

/**
 * Interstitial for the e-mailed withdraw-confirmation link. The signed link is
 * opened with GET (which inbox scanners / link prefetchers also do); this page
 * re-POSTs the signed URL back — auto-submitted, with a no-JS fallback button —
 * so the irreversible withdrawal is performed only on a real user interaction.
 * {@see \Ferienpass\CmsBundle\Controller\Page\AttendanceWithdrawConfirmController}.
 */
class AttendanceWithdrawConfirm extends AbstractPrivateFragmentController
{
    public function __construct(private readonly AttendanceRepository $attendances)
    {
    }

    public function __invoke(Request $request): Response
    {
        $attendance = $this->attendances->findOneBy(['uuid' => (string) $request->attributes->get('attendance')]);

        return $this->render('@Contao/fragment/attendance_withdraw_confirm.html.twig', [
            'attendance' => $attendance instanceof Attendance ? $attendance : null,
            'formAction' => (string) $request->attributes->get('formAction'),
        ]);
    }
}
