<?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\AdminBundle\Controller\Page;

use Ferienpass\CoreBundle\Entity\Offer\OfferInterface;
use Ferienpass\CoreBundle\Entity\Participant\ParticipantInterface;
use Ferienpass\CoreBundle\Repository\OfferRepositoryInterface;
use Ferienpass\CoreBundle\Repository\ParticipantRepositoryInterface;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Attribute\Route;

final class AttendanceCreateController extends AbstractController
{
    #[Route('/anmeldungen/neu', name: 'admin_attendances_create')]
    public function __invoke(Request $request, OfferRepositoryInterface $offers, ParticipantRepositoryInterface $participants): Response
    {
        $offer = null;
        $participant = null;

        if ($request->query->has('offer')) {
            $offer = $offers->findByUuid($uuid = (string) $request->query->get('offer'));
            if (!$offer instanceof OfferInterface) {
                throw $this->createNotFoundException();
            }
            $this->denyAccessUnlessGranted('participants.add', $offer);
        }

        if ($request->query->has('participant')) {
            $participant = $participants->findByUuid($uuid = (string) $request->query->get('participant'));
            if (!$participant instanceof ParticipantInterface) {
                throw $this->createNotFoundException();
            }
            // In case of participant-only context, keep permissive default; detailed ACLs handled inside component/actions
            $this->denyAccessUnlessGranted('view', $participant);
        }

        return $this->render('@FerienpassAdmin/page/attendances/create.html.twig', [
            'offer' => $offer,
            'participant' => $participant,
        ]);
    }
}
