<?php

namespace App\Controller;

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

#[Route('/content', name: 'content')]
class SnipContentController extends AbstractController
{
    public function __construct() {}

    #[Route('/compare/{to}/{from}', name: '_compare')]
    public function compare(SnipContent $to, ?SnipContent $from = null): Response
    {
        $this->denyAccessUnlessGranted(SnipVoter::VIEW, $to->snip);

        if ($from === null) {
            $from = $to->parent;
        }

        $diff = MyersDiff::buildDiffLines(
            SnipContentService::rebuildText($from),
            SnipContentService::rebuildText($to),
        );

        return $this->render('content/compare.html.twig', [
            'snip' => $to->snip,
            'diff' => $diff,
        ]);
    }
}
