<?php
namespace Modules\Payments;

use Gestoreasy\Core\Module\ModuleInterface;
use Gestoreasy\Core\Routing\Router;
use Modules\Payments\Controllers\WebhookController;
use Modules\Payments\Controllers\SettingsController;
use Gestoreasy\Core\Auth\AuthMiddleware;
use Gestoreasy\Core\Auth\Rbac;

class Module implements ModuleInterface {
    public function register(Router $router): void {
        $webhooks = new WebhookController();
        $settings = new SettingsController();

        // Endpoints públicos de webhook (não requerem Auth; validação via segredo e idempotência interna)
        $router->post('/api/payments/webhooks/{provider}', [$webhooks, 'handle'], []);
        // Healthcheck simples (útil para configurar webhook em provedores que testam URL)
        $router->get('/api/payments/webhooks/{provider}', [$webhooks, 'ping'], []);

        // Último evento recebido por provedor (para UI) — requer autenticação e permissão de leitura financeira
        $router->get('/api/payments/events/last/{provider}', [$webhooks, 'last'], [AuthMiddleware::requireAuth(), Rbac::requirePermissions(['financeiro.read'])]);

        // Simular evento de webhook (admin) — gera fluxo interno sem exigir assinatura
        $router->post('/api/payments/simulate/{provider}', [$webhooks, 'simulate'], [AuthMiddleware::requireAuth(), Rbac::requirePermissions(['financeiro.update'])]);

        // Configurações de provedores de pagamento (por tenant)
        $permManage = Rbac::requirePermissions(['financeiro.update']);
        $router->get('/api/payments/settings/{provider}', [$settings, 'get'], [AuthMiddleware::requireAuth(), $permManage]);
        $router->post('/api/payments/settings/{provider}', [$settings, 'save'], [AuthMiddleware::requireAuth(), $permManage]);
    }
    public function name(): string { return 'Payments'; }
    public function version(): string { return '0.1.0'; }
    public function dependencies(): array { return []; }
}