<?php
declare(strict_types=1);

namespace SwaggerBake\Test\TestCase\Lib\Attribute;

use Cake\Routing\Router;
use Cake\Routing\RouteBuilder;
use Cake\TestSuite\TestCase;
use SwaggerBake\Lib\Model\ModelScanner;
use SwaggerBake\Lib\Route\RouteScanner;
use SwaggerBake\Lib\Configuration;
use SwaggerBake\Lib\Swagger;

class OpenApiEntityAttributeTest extends TestCase
{
    /**
     * @var string[]
     */
    public array $fixtures = [
        'plugin.SwaggerBake.Employees'
    ];

    private Configuration $config;

    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-bare-bones.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);
    }

    public function test_entity_attribute(): void
    {
        $cakeRoute = new RouteScanner(new Router(), $this->config);

        $swagger = (new Swagger(new ModelScanner($cakeRoute, $this->config), $this->config))->build();

        $arr = json_decode($swagger->toString(), true);

        $employee = $arr['components']['schemas']['Employee'];

        $this->assertNotEmpty($employee['properties']['gender']['enum']);
        $this->assertEquals('female', $employee['properties']['gender']['example']);
        $this->assertEquals(3, $employee['properties']['last_name']['minLength']);
        $this->assertEquals(59, $employee['properties']['last_name']['maxLength']);
        $this->assertEquals('/\W/', $employee['properties']['last_name']['pattern']);
    }
}