<?php

use PHPUnit\Framework\TestCase;
use Slim\Factory\AppFactory;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Message\ResponseInterface;
use DI\Container;
use Psr\Container\ContainerInterface;
use App\View\Pages;
use App\Services\ApiService;

class IndexRouteTest extends TestCase
{
    protected $app;
    protected $container;
    
    
    protected function setUp(): void
    {

	$this->createMock(Pages::class);
	
	$this->container = new Container();
	$this->container->set('rest', "rest");
	$this->container->set('view', function(){
	    $view = $this->createMock(Pages::class);
	    return $view;
	});

	$this->container->set('api', function(){
	    $api = $this->createMock(ApiService::class);
	    return $api;
	});

	
	$this->container->set(App\Actions\MainAction::class, function (ContainerInterface $c) {
	    return new App\Actions\MainAction(
		$c->get('api'),
		$c->get('view')
	    );
	});
        // Инициализация приложения Slim для тестов
	AppFactory::setContainer($this->container);
        $this->app = AppFactory::create();
        
        // Добавляем тестовый маршрут
        $this->app->get('/ui', [App\Actions\MainAction::class, 'main']);
        
        /* $this->app = $app; */
    }

    public function testHelloRoute()
    {
        // Создаем mock-запрос
        $request = $this->createRequest('GET', '/ui');
	$responseFactory = new \Slim\Psr7\Factory\ResponseFactory();
	$response = $responseFactory->createResponse(200);
	$this->container->get('view')
	     ->method('render')
	     ->willReturn($response);

	$this->container->get('api')
	     ->method('status')
	     ->willReturn(json_decode(""));
        // Обрабатываем запрос
	
        $response = $this->app->handle($request);
        
        // Проверяем статус код
        $this->assertEquals(200, $response->getStatusCode());
        
        // Проверяем тело ответа
        /* $body = (string) $response->getBody();
	 * $this->assertEquals("Hello, world", $body); */
    }

    protected function createRequest(string $method, string $path): ServerRequestInterface
    {
        return (new \Slim\Psr7\Factory\ServerRequestFactory())
            ->createServerRequest($method, $path);
    }
}
