<?php

declare(strict_types=1);

namespace Ferienpass\AdminBundle\Tests\Controller;

use Ferienpass\AdminBundle\Components\Notification;
use Ferienpass\AdminBundle\Controller\FlashMessageController;
use PHPUnit\Framework\TestCase;
use Symfony\Component\DependencyInjection\Container;
use Twig\Environment;

final class FlashMessageControllerTest extends TestCase
{
    public function testInvokeRendersStreamTemplate(): void
    {
        $notification = new Notification();
        $notification->type = 'success';
        $notification->title = 'Done';
        $notification->message = 'It worked';

        $twig = $this->createMock(Environment::class);
        $twig->expects(self::once())
            ->method('render')
            ->with(
                '@FerienpassAdmin/flash_message.stream.html.twig',
                self::callback(function (array $params): bool {
                    return 'success' === $params['props']['type']
                        && 'Done' === $params['props']['title']
                        && 'It worked' === $params['props']['message'];
                }),
            )
            ->willReturn('<turbo-stream>');

        $container = new Container();
        $container->set('twig', $twig);

        $controller = new FlashMessageController();
        $controller->setContainer($container);

        $response = $controller($notification);

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

    public function testInvokePassesAllNotificationPropertiesAsProps(): void
    {
        $notification = new Notification();
        $notification->type = 'error';
        $notification->title = 'Error';
        $notification->message = 'Something went wrong';
        $notification->actionTitle = 'Retry';
        $notification->actionLink = '/retry';
        $notification->dismissible = false;
        $notification->dismissAfter = null;

        $twig = $this->createMock(Environment::class);
        $twig->expects(self::once())
            ->method('render')
            ->with(
                self::anything(),
                self::callback(function (array $params): bool {
                    $props = $params['props'];

                    return 'Retry' === $props['actionTitle']
                        && '/retry' === $props['actionLink']
                        && false === $props['dismissible']
                        && null === $props['dismissAfter'];
                }),
            )
            ->willReturn('');

        $container = new Container();
        $container->set('twig', $twig);

        $controller = new FlashMessageController();
        $controller->setContainer($container);

        $controller($notification);
    }
}
