<?php

namespace App\Controller;

use App\Service\UnsubscribeManager;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Attribute\Route;

class UnsubscribeController extends AbstractController
{
    #[Route('/unsubscribe/{email}/{token}', name: 'app_unsubscribe', methods: ['GET'])]
    public function __invoke(string $email, string $token, UnsubscribeManager $unsubscribeManager): Response
    {
        if (!$unsubscribeManager->isValidToken($email, $token)) {
            return $this->render('unsubscribe/invalid.html.twig');
        }

        $unsubscribeManager->unsubscribe($email);

        return $this->render('unsubscribe/success.html.twig', [
            'email' => $email,
        ]);
    }
}
