<?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\MainBundle\Tests\Authorization;

use Chill\MainBundle\Controller\ExportGenerationCreateFromSavedExportController;
use Chill\MainBundle\Entity\ExportGeneration;
use Chill\MainBundle\Entity\SavedExport;
use Chill\MainBundle\Entity\User;
use Chill\MainBundle\Export\Messenger\ExportRequestGenerationMessage;
use Chill\MainBundle\Security\Authorization\SavedExportVoter;
use Doctrine\ORM\EntityManagerInterface;
use PHPUnit\Framework\TestCase;
use Prophecy\Argument;
use Prophecy\PhpUnit\ProphecyTrait;
use Symfony\Component\Clock\MockClock;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\Messenger\Envelope;
use Symfony\Component\Messenger\MessageBusInterface;
use Symfony\Component\Security\Core\Security;
use Symfony\Component\Serializer\SerializerInterface;

/**
 * @internal
 *
 * @coversNothing
 */
class ExportGenerationCreateFromSavedExportControllerTest extends TestCase
{
    use ProphecyTrait;

    public function testInvoke(): void
    {
        $savedExport = new SavedExport();
        $savedExport->setOptions($exportOptions = ['test' => 'content'])->setExportAlias('dummy_export_alias');

        $user = new User();
        $reflection = new \ReflectionClass($user);
        $id = $reflection->getProperty('id');
        $id->setValue($user, 1);

        $security = $this->prophesize(Security::class);
        $security->isGranted(SavedExportVoter::GENERATE, $savedExport)->shouldBeCalled()->willReturn(true);
        $security->getUser()->willReturn($user);

        $entityManager = $this->prophesize(EntityManagerInterface::class);
        $entityManager->persist(Argument::that(
            static fn ($arg) => $arg instanceof ExportGeneration && $arg->getOptions() === $exportOptions && $arg->getSavedExport() === $savedExport,
        ))->shouldBeCalled();
        $entityManager->flush()->shouldBeCalled();

        $messenger = $this->prophesize(MessageBusInterface::class);
        $messenger->dispatch(Argument::type(ExportRequestGenerationMessage::class))->shouldBeCalled()->will(
            static fn (array $args): Envelope => new Envelope($args[0]),
        );

        $serializer = $this->prophesize(SerializerInterface::class);
        $serializer->serialize(Argument::type(ExportGeneration::class), 'json', ['groups' => ['read']])->shouldBeCalled()->willReturn('{"test": "export-generation"}');

        $controller = new ExportGenerationCreateFromSavedExportController(
            $security->reveal(),
            $entityManager->reveal(),
            $messenger->reveal(),
            new MockClock(),
            $serializer->reveal()
        );

        $response = $controller($savedExport);

        self::assertInstanceOf(JsonResponse::class, $response);
        self::assertEquals('{"test": "export-generation"}', $response->getContent());
    }
}
