<?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\Ticket\Handler\AddCommentCommandHandler;
use Chill\TicketBundle\Controller\AddCommentController;
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\Security\Core\Security;
use Symfony\Component\Serializer\SerializerInterface;
use Symfony\Component\Validator\Validator\ValidatorInterface;

/**
 * @internal
 *
 * @coversNothing
 */
class AddCommentControllerTest 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 testAddComment(): void
    {
        $controller = $this->buildController(willFlush: true);

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

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

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

    public function testAddCommentWithBlankContent(): void
    {
        $controller = $this->buildController(willFlush: false);

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

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

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

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

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

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

    private function buildController(bool $willFlush): AddCommentController
    {
        $security = $this->prophesize(Security::class);
        $security->isGranted('ROLE_USER')->willReturn(true);

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

        if ($willFlush) {
            $security->isGranted(CommentVoter::CREATE, Argument::type(Comment::class))->willReturn(true);
            $entityManager->persist(Argument::type(Comment::class))->shouldBeCalled();
            $entityManager->flush()->shouldBeCalled();
        }

        $commandHandler = new AddCommentCommandHandler($entityManager->reveal());

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