<?php

namespace Modules\Integracoes\MercadoLivre;

use Gestoreasy\Core\Module\ModuleInterface;
use Gestoreasy\Core\Routing\Router;
use Gestoreasy\Core\Http\AuthMiddleware;
use Gestoreasy\Core\Auth\Rbac;
use Modules\Integracoes\MercadoLivre\Controllers\MercadoLivreController;
use Modules\Integracoes\MercadoLivre\Controllers\MercadoLivreViewsController;

class Module implements ModuleInterface {
    public function register(Router $router): void {
        $apiCtrl = new MercadoLivreController();
        $viewsCtrl = new MercadoLivreViewsController();

        // Views
        $router->get('/integracoes/mercado-livre', [$viewsCtrl, 'dashboard'], [AuthMiddleware::requireAuthHtml(), Rbac::requirePermissions(['integracoes.mercadolivre.read'])]);
        $router->get('/integracoes/mercado-livre/config', [$viewsCtrl, 'config'], [AuthMiddleware::requireAuthHtml(), Rbac::requirePermissions(['integracoes.mercadolivre.read'])]);

        // OAuth Callback
        $router->get('/integracoes/mercado-livre/callback', [$apiCtrl, 'oauthCallback'], [AuthMiddleware::requireAuth()]);

        // API - Configuração
        $router->get('/api/mercadolivre/config', [$apiCtrl, 'getConfig'], [AuthMiddleware::requireAuth(), Rbac::requirePermissions(['integracoes.mercadolivre.read'])]);
        $router->post('/api/mercadolivre/config', [$apiCtrl, 'updateConfig'], [AuthMiddleware::requireAuth(), Rbac::requirePermissions(['integracoes.mercadolivre.update'])]);

        // API - Produtos
        $router->get('/api/mercadolivre/produtos', [$apiCtrl, 'listProdutos'], [AuthMiddleware::requireAuth(), Rbac::requirePermissions(['integracoes.mercadolivre.read'])]);
        $router->post('/api/mercadolivre/produtos/sincronizar', [$apiCtrl, 'sincronizarProdutos'], [AuthMiddleware::requireAuth(), Rbac::requirePermissions(['integracoes.mercadolivre.update'])]);
        $router->post('/api/mercadolivre/produtos/{id}/publicar', [$apiCtrl, 'publicarProduto'], [AuthMiddleware::requireAuth(), Rbac::requirePermissions(['integracoes.mercadolivre.update'])]);

        // API - Pedidos
        $router->get('/api/mercadolivre/pedidos', [$apiCtrl, 'listPedidos'], [AuthMiddleware::requireAuth(), Rbac::requirePermissions(['integracoes.mercadolivre.read'])]);
        $router->post('/api/mercadolivre/pedidos/sincronizar', [$apiCtrl, 'sincronizarPedidos'], [AuthMiddleware::requireAuth(), Rbac::requirePermissions(['integracoes.mercadolivre.update'])]);
        $router->post('/api/mercadolivre/pedidos/{id}/importar', [$apiCtrl, 'importarPedido'], [AuthMiddleware::requireAuth(), Rbac::requirePermissions(['integracoes.mercadolivre.update'])]);

        // API - Estoque
        $router->post('/api/mercadolivre/estoque/sincronizar', [$apiCtrl, 'sincronizarEstoque'], [AuthMiddleware::requireAuth(), Rbac::requirePermissions(['integracoes.mercadolivre.update'])]);

        // Webhooks (público, validação interna)
        $router->post('/api/mercadolivre/webhook', [$apiCtrl, 'webhook'], []);
    }
}

