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

use Ferienpass\CmsBundle\Controller\AbstractController;
use Ferienpass\CmsBundle\Fragment\FragmentReference;
use Ferienpass\CoreBundle\Entity\Payment;
use Ferienpass\CoreBundle\Entity\User;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Attribute\Route;

final class PaymentReceiptController extends AbstractController
{
    #[Route('/zahlungen/beleg/{id}', name: 'cms_payment_receipt', defaults: ['_requires_payments' => true])]
    public function __invoke(Payment $payment, Request $request): Response
    {
        $this->initializeContaoFramework();

        $user = $this->getUser();
        if (!$user instanceof User || $payment->getUser() !== $user) {
            throw $this->createAccessDeniedException();
        }

        return $this->markPageAsPrivate(
            $this->createPageBuilder()
                ->addFragment('main', new FragmentReference('ferienpass.fragment.payment_receipt_fragment', [
                    'payment' => $payment,
                ]))
                ->getResponse()
        );
    }
}
