<?php

namespace Modules\Integracoes\WooCommerce;

use Gestoreasy\Core\Module\ModuleInterface;
use Gestoreasy\Core\Routing\Router;
use Gestoreasy\Core\Http\AuthMiddleware;
use Gestoreasy\Core\Auth\Rbac;
use Modules\Integracoes\WooCommerce\Controllers\WooCommerceController;
use Modules\Integracoes\WooCommerce\Controllers\WooCommerceViewsController;

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

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

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

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

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

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

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

    public function name(): string {
        return 'WooCommerce';
    }

    public function version(): string {
        return '1.0.0';
    }

    public function dependencies(): array {
        return ['Vendas', 'Produtos', 'Estoque'];
    }
}

