<?php

namespace SwaggerBake\Test\TestCase\Lib;

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 SwaggerSchemaTest 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');
            $builder->resources('EmployeeSalaries');
        });

        $this->config = new Configuration([
            'prefix' => '/',
            'yml' => '/config/swagger-with-existing.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_employee_table_properties(): 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);

        $this->assertArrayHasKey('Employee', $arr['components']['schemas']);
        $employee = $arr['components']['schemas']['Employee'];

        $this->assertArrayHasKey('birth_date', $employee['properties']);

        $this->assertTrue($employee['properties']['id']['readOnly']);
        $this->assertEquals('integer', $employee['properties']['id']['type']);
        $this->assertEquals('int64', $employee['properties']['id']['format']);


        $this->assertEquals('date', $employee['properties']['birth_date']['format']);
        $this->assertIsArray($employee['properties']['gender']['enum']);
        $this->assertEquals('date', $employee['properties']['hire_date']['format']);

        $this->assertTrue($employee['properties']['read']['readOnly']);
        $this->assertArrayNotHasKey('write', $employee['properties']);
        $this->assertArrayNotHasKey('hide', $employee['properties']);

        foreach (['birth_date', 'first_name', 'last_name', 'gender', 'hire_date',] as $property) {
            $this->assertEquals('string', $employee['properties'][$property]['type'], "$property is not string");
            $this->assertTrue(!isset($employee['properties'][$property]['readOnly']), "$property should !readOnly");
            $this->assertTrue(!isset($employee['properties'][$property]['writeOnly']), "$property should !writeOnly");
        }
    }

    public function test_yml_schema_takes_precedence(): 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);

        $this->assertArrayHasKey('EmployeeSalaries', $arr['components']['schemas']);
        $employee = $arr['components']['schemas']['EmployeeSalaries'];

        $this->assertEquals('Test YML schema cannot be overwritten', $employee['description']);
    }
}