<?php

namespace SwaggerBake\Test\TestCase\Lib\Operation;

use Cake\Routing\RouteBuilder;
use Cake\Routing\Router;
use Cake\TestSuite\TestCase;
use SwaggerBake\Lib\Configuration;
use SwaggerBake\Lib\SwaggerFactory;
use SwaggerBake\Lib\OpenApi\Operation;
use SwaggerBake\Lib\Operation\OperationResponse;
use SwaggerBake\Lib\Route\RouteScanner;
use SwaggerBakeTest\App\Controller\EmployeesController;

class OperationResponseYamlTest extends TestCase
{
    public array $fixtures = [
        'plugin.SwaggerBake.Employees',
    ];

    /**
     * @var Router
     */
    private $router;

    /**
     * @var Configuration
     */
    private $config;

    /**
     * @var array
     */
    private $routes;

    public function setUp(): void
    {
        parent::setUp(); // TODO: Change the autogenerated stub
        Router::createRouteBuilder('/')->scope('/', function (RouteBuilder $builder) {
            $builder->setExtensions(['json']);
            $builder->resources('Employees');
        });

        $this->config = new Configuration([
            'prefix' => '/',
            'yml' => '/config/swagger-overwrite-model.yml',
            'json' => '/webroot/swagger.json',
            'webPath' => '/swagger.json',
            'hotReload' => false,
            'exceptionSchema' => 'Exception',
            'requestAccepts' => ['application/x-www-form-urlencoded'],
            'responseContentTypes' => ['application/json'],
            'namespaces' => [
                'controllers' => ['\SwaggerBakeTest\App\\'],
                'entities' => ['\SwaggerBakeTest\App\\'],
                'tables' => ['\SwaggerBakeTest\App\\'],
            ]
        ], SWAGGER_BAKE_TEST_APP);

        if (empty($this->routes)) {
            $cakeRoute = new RouteScanner(new Router(), $this->config);
            $this->routes = $cakeRoute->getRoutes();
        }
    }

    /**
     * @see https://github.com/cnizzardini/cakephp-swagger-bake/issues/274
     */
    public function test_yaml_schema_overwriting_cakephp_model_schema(): void
    {
        $route = $this->routes['employees:view'];
        $swagger = (new SwaggerFactory($this->config, new RouteScanner(new Router(), $this->config)))
            ->create()
            ->build();

        $operationResponse = new OperationResponse(
            $swagger,
            $this->config,
            new Operation('hello', 'get'),
            $route,
            $swagger->getArray()['components']['schemas']['Employee'],
            (new \ReflectionClass(EmployeesController::class))->getMethod('view')
        );

        $schema = $operationResponse
            ->getOperationWithResponses()
            ->getResponseByCode('200')
            ->getContentByMimeType('application/json')
            ->getSchema();

        $this->assertEquals('#/components/schemas/Employee', $schema->getRefEntity());
    }
}