<?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\CmsBundle\Controller\AbstractController;
use Ferienpass\CoreBundle\Entity\Edition;
use Ferienpass\CoreBundle\HttpCache\CacheTag;
use Ferienpass\CoreBundle\Repository\EditionRepository;
use Symfony\Component\HttpFoundation\Response;

/**
 * Renders an edition's `welcomeText` (markdown) above the offer list on
 * the `/o/{alias}` landing page. Registered as `ferienpass.fragment.edition_welcome`.
 */
final class EditionWelcomeController extends AbstractController
{
    public function __construct(private readonly EditionRepository $editions)
    {
    }

    public function __invoke(int $edition): Response
    {
        $editionEntity = $this->editions->find($edition);
        if (!$editionEntity instanceof Edition || '' === trim((string) $editionEntity->getWelcomeText())) {
            return new Response('');
        }

        $this->tagResponse(CacheTag::edition($editionEntity));

        return $this->render('@Contao/fragment/edition_welcome.html.twig', [
            'edition' => $editionEntity,
        ]);
    }
}
