<?php

declare(strict_types=1);

namespace App\Framework\OpenApi;

/**
 * Represents a complete OpenAPI specification
 */
final readonly class OpenApiSpec
{
    public function __construct(
        public OpenApiInfo $info,
        public string $openapi = '3.0.3',
        public array $servers = [],
        public array $paths = [],
        public array $components = [],
        public array $security = [],
        public array $tags = [],
    ) {
    }

    public function toArray(): array
    {
        $spec = [
            'openapi' => $this->openapi,
            'info' => $this->info->toArray(),
        ];

        if (! empty($this->servers)) {
            $spec['servers'] = $this->servers;
        }

        if (! empty($this->paths)) {
            $spec['paths'] = $this->paths;
        }

        if (! empty($this->components)) {
            $spec['components'] = $this->components;
        }

        if (! empty($this->security)) {
            $spec['security'] = $this->security;
        }

        if (! empty($this->tags)) {
            $spec['tags'] = $this->tags;
        }

        return $spec;
    }

    public function toJson(): string
    {
        return json_encode($this->toArray(), JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES);
    }
}
