<?php

namespace Modules\APIPublica\Controllers;

use Gestoreasy\Core\Http\Request;
use Gestoreasy\Core\Http\Response;

class APIDocsController {
    public function swagger(Request $req): Response {
        $html = <<<HTML
<!DOCTYPE html>
<html>
<head>
    <title>API Documentation - GestorEasy</title>
    <link rel="stylesheet" type="text/css" href="https://unpkg.com/swagger-ui-dist@4.15.5/swagger-ui.css" />
    <style>
        body { margin: 0; }
    </style>
</head>
<body>
    <div id="swagger-ui"></div>
    <script src="https://unpkg.com/swagger-ui-dist@4.15.5/swagger-ui-bundle.js"></script>
    <script src="https://unpkg.com/swagger-ui-dist@4.15.5/swagger-ui-standalone-preset.js"></script>
    <script>
        window.onload = function() {
            const ui = SwaggerUIBundle({
                url: "/api/docs/openapi.json",
                dom_id: '#swagger-ui',
                presets: [
                    SwaggerUIBundle.presets.apis,
                    SwaggerUIStandalonePreset
                ],
                layout: "StandaloneLayout"
            });
        };
    </script>
</body>
</html>
HTML;
        return new Response(200, [], $html);
    }

    public function openapi(Request $req): Response {
        $openapi = [
            'openapi' => '3.0.0',
            'info' => [
                'title' => 'GestorEasy API',
                'version' => '1.0.0',
                'description' => 'API REST completa do sistema ERP GestorEasy'
            ],
            'servers' => [
                ['url' => '/api/v1', 'description' => 'API v1']
            ],
            'paths' => [
                '/products' => [
                    'get' => [
                        'summary' => 'Listar produtos',
                        'responses' => [
                            '200' => ['description' => 'Lista de produtos']
                        ]
                    ],
                    'post' => [
                        'summary' => 'Criar produto',
                        'requestBody' => [
                            'required' => true,
                            'content' => [
                                'application/json' => [
                                    'schema' => [
                                        'type' => 'object',
                                        'properties' => [
                                            'codigo' => ['type' => 'string'],
                                            'nome' => ['type' => 'string'],
                                            'preco' => ['type' => 'number']
                                        ]
                                    ]
                                ]
                            ]
                        ],
                        'responses' => [
                            '201' => ['description' => 'Produto criado']
                        ]
                    ]
                ],
                '/products/{id}' => [
                    'get' => [
                        'summary' => 'Obter produto',
                        'parameters' => [
                            ['name' => 'id', 'in' => 'path', 'required' => true, 'schema' => ['type' => 'integer']]
                        ],
                        'responses' => [
                            '200' => ['description' => 'Produto encontrado'],
                            '404' => ['description' => 'Produto não encontrado']
                        ]
                    ]
                ],
                '/customers' => [
                    'get' => [
                        'summary' => 'Listar clientes',
                        'responses' => [
                            '200' => ['description' => 'Lista de clientes']
                        ]
                    ]
                ],
                '/webhooks' => [
                    'get' => [
                        'summary' => 'Listar webhooks',
                        'responses' => [
                            '200' => ['description' => 'Lista de webhooks']
                        ]
                    ],
                    'post' => [
                        'summary' => 'Criar webhook',
                        'responses' => [
                            '201' => ['description' => 'Webhook criado']
                        ]
                    ]
                ]
            ],
            'components' => [
                'securitySchemes' => [
                    'ApiKeyAuth' => [
                        'type' => 'apiKey',
                        'in' => 'header',
                        'name' => 'X-API-Key'
                    ]
                ]
            ]
        ];

        return Response::json($openapi);
    }
}

