<?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 Doctrine\Persistence\ManagerRegistry;
use Ferienpass\AdminBundle\Breadcrumb\Breadcrumb;
use Ferienpass\AdminBundle\Controller\TagsResponses;
use Ferienpass\AdminBundle\Routing\Base58;
use Ferienpass\CoreBundle\ApplicationSystem\ApplicationSystems;
use Ferienpass\CoreBundle\Entity\HostConsent;
use Ferienpass\CoreBundle\Entity\Offer\OfferInterface;
use Ferienpass\CoreBundle\Entity\User;
use Ferienpass\CoreBundle\Export\ParticipantList\WordExport;
use Ferienpass\CoreBundle\HttpCache\CacheTag;
use Ferienpass\CoreBundle\Repository\HostConsentRepository;
use Ferienpass\CoreBundle\Repository\OfferRepositoryInterface;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\DependencyInjection\Attribute\Autowire;
use Symfony\Component\Form\Extension\Core\Type\CheckboxType;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\Session\Session;
use Symfony\Component\Routing\Attribute\Route;
use Symfony\Component\Routing\Requirement\Requirement;
use Symfony\Component\Security\Http\Attribute\CurrentUser;
use Symfony\Component\Uid\Uuid;

#[Route('/angebote/{uuid}/zuordnen', requirements: ['uuid' => Base58::UUID])]
class OfferAssignController extends AbstractController
{
    use TagsResponses;

    public function __construct(private readonly WordExport $wordExport)
    {
    }

    #[Route('', name: 'admin_offer_assign', requirements: ['uuid' => Base58::UUID])]
    #[Route('', name: 'admin_offer_assign_legacy', requirements: ['uuid' => Requirement::UUID])]
    public function __invoke(Uuid $uuid, #[CurrentUser] User $user, Request $request, OfferRepositoryInterface $offers, Session $session, ManagerRegistry $doctrine, HostConsentRepository $consents, Breadcrumb $breadcrumb, ApplicationSystems $applicationSystems, #[Autowire(param: 'ferienpass_admin.privacy_consent_text')] string $consentText): Response
    {
        /** @var OfferInterface $offer */
        if (!($offer = $offers->findByUuid($uuid)) instanceof OfferInterface) {
            throw $this->createNotFoundException();
        }

        if ($offer->getEdition()->hostsCanAssign()) {
            $this->denyAccessUnlessGranted('participants.view', $offer);
        } else {
            $this->denyAccessUnlessGranted('ROLE_ADMIN');
        }

        if ('' !== $consentText && !$consents->findValid($user) instanceof HostConsent) {
            $this->tagResponse(CacheTag::offer($offer));

            return $this->render('@FerienpassAdmin/page/missing_privacy_statement.html.twig', [
                'breadcrumb' => $breadcrumb->generate(
                    ['offers.title', ['route' => 'admin_offers_index', 'routeParameters' => ['edition' => $offer->getEdition()->getAlias()]]],
                    [$offer->getEdition()->getName(), ['route' => 'admin_offers_index', 'routeParameters' => ['edition' => $offer->getEdition()->getAlias()]]],
                    $offer->getName()
                ),
            ]);
        }

        $autoCommit = $session->get('admin--auto-assign', false);
        $toggleMode = $this->createFormBuilder(['auto' => $autoCommit])
            ->add('auto', CheckboxType::class, ['false_values' => ['']])
            ->getForm()
        ;

        $toggleMode->handleRequest($request);
        if ($toggleMode->isSubmitted() && $toggleMode->isValid()) {
            $autoCommit = $toggleMode->get('auto')->getData();

            $session->set('admin--auto-assign', $autoCommit);
        }

        $this->tagResponse(CacheTag::offer($offer));

        return $this->render('@FerienpassAdmin/page/offers/assign.html.twig', [
            'offer' => $offer,
            'toggleMode' => $toggleMode,
            'autoCommit' => $autoCommit,
            'hasWordExport' => $this->wordExport->hasTemplate(),
            'breadcrumb' => $breadcrumb->generate(
                ['offers.title', ['route' => 'admin_offers_index', 'routeParameters' => array_filter(['edition' => $offer->getEdition()->getAlias()])]],
                [$offer->getEdition()->getName(), ['route' => 'admin_offers_index', 'routeParameters' => ['edition' => $offer->getEdition()->getAlias()]]],
                $offer->getName()
            ),
        ]);
    }
}
