<?php
namespace Modules\AsaasAddon;

use Gestoreasy\Core\Module\ModuleInterface;
use Gestoreasy\Core\Routing\Router;
use Gestoreasy\Core\Auth\AuthMiddleware;
use Gestoreasy\Core\Auth\Rbac;
use Modules\AsaasAddon\Controllers\AsaasController;
use Modules\AsaasAddon\Controllers\AsaasViewsController;

class Module implements ModuleInterface {
    public function register(Router $router): void {
        $api = new AsaasController();
        $views = new AsaasViewsController();

        // Painel HTML
        $router->get('/addons/asaas', [$views, 'viewIndex'], [AuthMiddleware::requireAuthHtml(), Rbac::requirePermissions(['addons.asaas.read'])]);

        // Configuração (API)
        $router->get('/api/addons/asaas/config', [$api, 'getConfig'], [AuthMiddleware::requireAuth(), Rbac::requirePermissions(['addons.asaas.read'])]);
        $router->post('/api/addons/asaas/config', [$api, 'saveConfig'], [AuthMiddleware::requireAuth(), Rbac::requirePermissions(['addons.asaas.manage'])]);

        // Geração de link de pagamento associado a venda
        $router->post('/api/addons/asaas/payment-links', [$api, 'generatePaymentLink'], [AuthMiddleware::requireAuth(), Rbac::requirePermissions(['addons.asaas.manage'])]);
        $router->get('/api/addons/asaas/payment-links/{id}', [$api, 'getPaymentLinkById'], [AuthMiddleware::requireAuth(), Rbac::requirePermissions(['addons.asaas.read'])]);

        // Geração de link por Pedido (Vendas)
        $router->post('/api/addons/asaas/orders/{id}/payment-link', [$api, 'generatePaymentLinkForOrder'], [AuthMiddleware::requireAuth(), Rbac::requirePermissions(['addons.asaas.manage'])]);

        // Endpoints básicos de integração
        $router->get('/api/addons/asaas/customers', [$api, 'listCustomers'], [AuthMiddleware::requireAuth(), Rbac::requirePermissions(['addons.asaas.read'])]);
        $router->get('/api/addons/asaas/payments', [$api, 'listPayments'], [AuthMiddleware::requireAuth(), Rbac::requirePermissions(['addons.asaas.read'])]);
        $router->post('/api/addons/asaas/payments', [$api, 'createPayment'], [AuthMiddleware::requireAuth(), Rbac::requirePermissions(['addons.asaas.manage'])]);
        $router->post('/api/addons/asaas/payments/{id}/refund', [$api, 'refundPayment'], [AuthMiddleware::requireAuth(), Rbac::requirePermissions(['addons.asaas.manage'])]);
    }

    public function name(): string { return 'AsaasAddon'; }
    public function version(): string { return '0.1.0'; }
    public function dependencies(): array { return ['Auth']; }
}