<?php

namespace Modules\APIPublica;

use Gestoreasy\Core\Module\ModuleInterface;
use Gestoreasy\Core\Routing\Router;
use Gestoreasy\Core\Auth\AuthMiddleware;
use Gestoreasy\Core\Auth\Rbac;
use Modules\APIPublica\Controllers\APIController;
use Modules\APIPublica\Controllers\WebhooksController;
use Modules\APIPublica\Controllers\APIDocsController;

class Module implements ModuleInterface {
    public function register(Router $router): void {
        $apiCtrl = new APIController();
        $webhooksCtrl = new WebhooksController();
        $docsCtrl = new APIDocsController();

        // Documentação da API
        $router->get('/api/docs', [$docsCtrl, 'swagger'], [AuthMiddleware::requireAuthHtml()]);
        $router->get('/api/docs/openapi.json', [$docsCtrl, 'openapi'], [AuthMiddleware::requireAuth()]);

        // API Pública v1
        $router->get('/api/v1/products', [$apiCtrl, 'listProducts'], [AuthMiddleware::requireAuthOrApiKey()]);
        $router->get('/api/v1/products/{id}', [$apiCtrl, 'getProduct'], [AuthMiddleware::requireAuthOrApiKey()]);
        $router->post('/api/v1/products', [$apiCtrl, 'createProduct'], [AuthMiddleware::requireAuthOrApiKey(), Rbac::requirePermissions(['produtos.create'])]);
        $router->put('/api/v1/products/{id}', [$apiCtrl, 'updateProduct'], [AuthMiddleware::requireAuthOrApiKey(), Rbac::requirePermissions(['produtos.update'])]);
        $router->delete('/api/v1/products/{id}', [$apiCtrl, 'deleteProduct'], [AuthMiddleware::requireAuthOrApiKey(), Rbac::requirePermissions(['produtos.delete'])]);

        $router->get('/api/v1/customers', [$apiCtrl, 'listCustomers'], [AuthMiddleware::requireAuthOrApiKey()]);
        $router->get('/api/v1/customers/{id}', [$apiCtrl, 'getCustomer'], [AuthMiddleware::requireAuthOrApiKey()]);
        $router->post('/api/v1/customers', [$apiCtrl, 'createCustomer'], [AuthMiddleware::requireAuthOrApiKey()]);
        $router->put('/api/v1/customers/{id}', [$apiCtrl, 'updateCustomer'], [AuthMiddleware::requireAuthOrApiKey()]);

        $router->get('/api/v1/orders', [$apiCtrl, 'listOrders'], [AuthMiddleware::requireAuthOrApiKey()]);
        $router->get('/api/v1/orders/{id}', [$apiCtrl, 'getOrder'], [AuthMiddleware::requireAuthOrApiKey()]);
        $router->post('/api/v1/orders', [$apiCtrl, 'createOrder'], [AuthMiddleware::requireAuthOrApiKey()]);

        $router->get('/api/v1/inventory', [$apiCtrl, 'getInventory'], [AuthMiddleware::requireAuthOrApiKey()]);
        $router->get('/api/v1/inventory/{product_id}', [$apiCtrl, 'getProductInventory'], [AuthMiddleware::requireAuthOrApiKey()]);

        // Webhooks
        $router->get('/api/v1/webhooks', [$webhooksCtrl, 'list'], [AuthMiddleware::requireAuthOrApiKey()]);
        $router->post('/api/v1/webhooks', [$webhooksCtrl, 'create'], [AuthMiddleware::requireAuthOrApiKey()]);
        $router->put('/api/v1/webhooks/{id}', [$webhooksCtrl, 'update'], [AuthMiddleware::requireAuthOrApiKey()]);
        $router->delete('/api/v1/webhooks/{id}', [$webhooksCtrl, 'delete'], [AuthMiddleware::requireAuthOrApiKey()]);
        $router->post('/api/v1/webhooks/{id}/test', [$webhooksCtrl, 'test'], [AuthMiddleware::requireAuthOrApiKey()]);
    }

    public function name(): string { return 'APIPublica'; }
    public function version(): string { return '1.0.0'; }
    public function dependencies(): array { return []; }
}

