<?php

namespace App\Controller;

use App\Entity\Snip;
use App\Entity\SnipContent;
use App\Security\Voter\SnipVoter;
use App\Service\SnipContent\SnipContentService;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Attribute\Route;

#[Route('/version/{snip}', name: 'version')]
class VersionController extends AbstractController
{
    public function __construct(
        private readonly SnipContentService $contentService,
    ) {}

    #[Route('/', name: '_index')]
    public function index(Snip $snip): Response
    {
        $this->denyAccessUnlessGranted(SnipVoter::EDIT, $snip);

        return $this->render('version/index.html.twig', [
            'snip' => $snip,
        ]);
    }

    #[Route('/set/{version}', name: '_set')]
    public function set(Snip $snip, SnipContent $version): Response
    {
        $this->denyAccessUnlessGranted(SnipVoter::EDIT, $snip);

        $this->contentService->setVersion($snip, $version);
        $this->addFlash('success', 'Snip version set to ' . $version->id);
        return $this->redirectToRoute('snip_single', ['snip' => $snip->id]);
    }
}
