<?php

declare(strict_types=1);

/*
 * This file is part of the VITICORE2 package.
 *
 * (c) CP Creation <web@cpcreation.fr>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

namespace CpCreation\VitiCore\Bridge\Symfony\Bundle\Controller\Site;

use CpCreation\VitiCore\Marketing\Model\ReviewInvitation;
use CpCreation\VitiCore\Marketing\Repository\ReviewInvitationRepository;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Response;

final class ReviewSolicitationOptOutController extends AbstractController
{
    public function __construct(
        private readonly ReviewInvitationRepository $invitationRepository,
    ) {}

    public function __invoke(string $token): Response
    {
        if (!$this->invitationRepository->findOneByToken($token) instanceof ReviewInvitation) {
            throw $this->createNotFoundException();
        }

        return $this->protectTokenResponse($this->render('marketing/review/opt_out.html.twig', [
            'token' => $token,
        ]));
    }

    private function protectTokenResponse(Response $response): Response
    {
        $response->headers->set('X-Robots-Tag', 'noindex, nofollow, noarchive');
        $response->headers->set('Referrer-Policy', 'no-referrer');
        $response->headers->set('Cache-Control', 'private, no-store');

        return $response;
    }
}
