<?php

declare(strict_types=1);

/*
 * Chill is a software for social workers
 *
 * For the full copyright and license information, please view
 * the LICENSE file that was distributed with this source code.
 */

namespace Chill\TicketBundle\Tests\Controller;

use Chill\TicketBundle\Action\Comment\Handler\UpdateCommentContentCommandHandlerInterface;
use Chill\TicketBundle\Controller\UpdateCommentController;
use Chill\TicketBundle\Entity\Comment;
use Chill\TicketBundle\Entity\Ticket;
use Chill\TicketBundle\Security\Voter\CommentVoter;
use Doctrine\ORM\EntityManagerInterface;
use Prophecy\Argument;
use Prophecy\PhpUnit\ProphecyTrait;
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException;
use Symfony\Component\Security\Core\Security;
use Symfony\Component\Serializer\SerializerInterface;
use Symfony\Component\Validator\Validator\ValidatorInterface;

/**
 * @internal
 *
 * @coversNothing
 */
class UpdateCommentControllerTest extends KernelTestCase
{
    use ProphecyTrait;

    private SerializerInterface $serializer;
    private ValidatorInterface $validator;

    protected function setUp(): void
    {
        self::bootKernel();
        $this->validator = self::getContainer()->get(ValidatorInterface::class);
        $this->serializer = self::getContainer()->get(SerializerInterface::class);
    }

    public function testUpdateComment(): void
    {
        $ticket = new Ticket();
        $comment = new Comment('initial content', $ticket);
        $controller = $this->buildController(willFlush: true, isGranted: true, comment: $comment);

        $request = new Request(content: <<<'JSON'
            {"content":  "updated content"}
            JSON);

        $response = $controller->__invoke($comment, $request);

        self::assertEquals(200, $response->getStatusCode());
    }

    public function testUpdateCommentWithBlankContent(): void
    {
        $ticket = new Ticket();
        $comment = new Comment('initial content', $ticket);
        $controller = $this->buildController(willFlush: false, isGranted: true, comment: $comment);

        $request = new Request(content: <<<'JSON'
            {"content":  ""}
            JSON);

        $response = $controller->__invoke($comment, $request);

        self::assertEquals(422, $response->getStatusCode());
    }

    public function testUpdateCommentWithNullContent(): void
    {
        $ticket = new Ticket();
        $comment = new Comment('initial content', $ticket);
        $controller = $this->buildController(willFlush: false, isGranted: true, comment: $comment);

        $request = new Request(content: <<<'JSON'
            {"content":  null}
            JSON);

        $response = $controller->__invoke($comment, $request);

        self::assertEquals(422, $response->getStatusCode());
    }

    public function testUpdateCommentWithoutAuthorization(): void
    {
        $ticket = new Ticket();
        $comment = new Comment('initial content', $ticket);
        $controller = $this->buildController(willFlush: false, isGranted: false, comment: $comment);

        $request = new Request(content: <<<'JSON'
            {"content":  "updated content"}
            JSON);

        $this->expectException(AccessDeniedHttpException::class);
        $this->expectExceptionMessage('You are not allowed to edit this comment.');

        $controller->__invoke($comment, $request);
    }

    private function buildController(bool $willFlush, bool $isGranted, Comment $comment): UpdateCommentController
    {
        $security = $this->prophesize(Security::class);
        $security->isGranted(CommentVoter::EDIT, $comment)->willReturn($isGranted);

        $entityManager = $this->prophesize(EntityManagerInterface::class);

        if ($willFlush) {
            $entityManager->flush()->shouldBeCalled();
        }

        $commandHandler = $this->prophesize(UpdateCommentContentCommandHandlerInterface::class);

        if ($isGranted && $willFlush) {
            $commandHandler->handle($comment, Argument::any())->shouldBeCalled();
        }

        return new UpdateCommentController(
            $security->reveal(),
            $this->serializer,
            $this->validator,
            $commandHandler->reveal(),
            $entityManager->reveal(),
        );
    }
}
