<?php
namespace Modules\Ecommerce;

use Gestoreasy\Core\Module\ModuleInterface;
use Gestoreasy\Core\Routing\Router;
use Gestoreasy\Core\Http\Request;
use Gestoreasy\Core\Http\Response;
use Gestoreasy\Core\Tenant\ConnectionManager;
use Gestoreasy\Core\Auth\AuthMiddleware;
use Gestoreasy\Core\Auth\Rbac;
use Modules\Ecommerce\Controllers\EcommerceController;
use Modules\Ecommerce\Controllers\EcommercePublicController;
use Modules\Ecommerce\Controllers\EcommerceViewsController;
use Modules\Ecommerce\Controllers\EcommerceCategoriasController;

class Module implements ModuleInterface {
    public function register(Router $router): void {
        $ctrl = new EcommerceController();
        $publicCtrl = new EcommercePublicController();
        $viewsCtrl = new EcommerceViewsController();
        $categoriasCtrl = new EcommerceCategoriasController();

        // Views administrativas (HTML)
        $router->get('/vendas/ecommerce', [$viewsCtrl, 'viewIndex'], [AuthMiddleware::requireAuthHtml(), Rbac::requirePermissions(['ecommerce.read'])]);
        $router->get('/vendas/ecommerce/config', [$viewsCtrl, 'viewConfig'], [AuthMiddleware::requireAuthHtml(), Rbac::requirePermissions(['ecommerce.update'])]);
        $router->get('/vendas/ecommerce/templates', [$viewsCtrl, 'viewTemplates'], [AuthMiddleware::requireAuthHtml(), Rbac::requirePermissions(['ecommerce.update'])]);
        $router->get('/vendas/ecommerce/dominios', [$viewsCtrl, 'viewDominios'], [AuthMiddleware::requireAuthHtml(), Rbac::requirePermissions(['ecommerce.update'])]);
        $router->get('/vendas/ecommerce/pagamentos', [$viewsCtrl, 'viewPagamentos'], [AuthMiddleware::requireAuthHtml(), Rbac::requirePermissions(['ecommerce.update'])]);
        $router->get('/vendas/ecommerce/pedidos', [$viewsCtrl, 'viewPedidos'], [AuthMiddleware::requireAuthHtml(), Rbac::requirePermissions(['ecommerce.read'])]);
        $router->get('/vendas/ecommerce/pedidos/{id}', [$viewsCtrl, 'viewPedido'], [AuthMiddleware::requireAuthHtml(), Rbac::requirePermissions(['ecommerce.read'])]);

        // API - Configurações
        $router->get('/api/ecommerce/config', [$ctrl, 'getConfig'], [AuthMiddleware::requireAuth(), Rbac::requirePermissions(['ecommerce.read'])]);
        $router->put('/api/ecommerce/config', [$ctrl, 'updateConfig'], [AuthMiddleware::requireAuth(), Rbac::requirePermissions(['ecommerce.update'])]);
        
        // API - Templates
        $router->get('/api/ecommerce/templates', [$ctrl, 'listTemplates'], [AuthMiddleware::requireAuth(), Rbac::requirePermissions(['ecommerce.read'])]);
        $router->get('/api/ecommerce/templates/{id}', [$ctrl, 'getTemplate'], [AuthMiddleware::requireAuth(), Rbac::requirePermissions(['ecommerce.read'])]);
        $router->put('/api/ecommerce/template', [$ctrl, 'setTemplate'], [AuthMiddleware::requireAuth(), Rbac::requirePermissions(['ecommerce.update'])]);
        
        // API - Domínios
        $router->get('/api/ecommerce/dominios', [$ctrl, 'listDominios'], [AuthMiddleware::requireAuth(), Rbac::requirePermissions(['ecommerce.read'])]);
        $router->post('/api/ecommerce/dominios', [$ctrl, 'createDominio'], [AuthMiddleware::requireAuth(), Rbac::requirePermissions(['ecommerce.update'])]);
        $router->put('/api/ecommerce/dominios/{id}', [$ctrl, 'updateDominio'], [AuthMiddleware::requireAuth(), Rbac::requirePermissions(['ecommerce.update'])]);
        $router->delete('/api/ecommerce/dominios/{id}', [$ctrl, 'deleteDominio'], [AuthMiddleware::requireAuth(), Rbac::requirePermissions(['ecommerce.update'])]);
        
        // API - Pagamentos
        $router->get('/api/ecommerce/pagamentos', [$ctrl, 'getPagamentos'], [AuthMiddleware::requireAuth(), Rbac::requirePermissions(['ecommerce.read'])]);
        $router->put('/api/ecommerce/pagamentos/mercadopago', [$ctrl, 'updateMercadoPago'], [AuthMiddleware::requireAuth(), Rbac::requirePermissions(['ecommerce.update'])]);
        $router->put('/api/ecommerce/pagamentos/pagbank', [$ctrl, 'updatePagBank'], [AuthMiddleware::requireAuth(), Rbac::requirePermissions(['ecommerce.update'])]);
        $router->put('/api/ecommerce/pagamentos/outros', [$ctrl, 'updateOutrosPagamentos'], [AuthMiddleware::requireAuth(), Rbac::requirePermissions(['ecommerce.update'])]);
        
        // API - Produtos (sincronização)
        $router->get('/api/ecommerce/produtos', [$ctrl, 'listProdutos'], [AuthMiddleware::requireAuth(), Rbac::requirePermissions(['ecommerce.read', 'produtos.read'])]);
        $router->put('/api/ecommerce/produtos/{id}/visibilidade', [$ctrl, 'toggleProdutoVisibilidade'], [AuthMiddleware::requireAuth(), Rbac::requirePermissions(['ecommerce.update', 'produtos.update'])]);
        $router->get('/api/ecommerce/produtos/sincronizar', [$ctrl, 'sincronizarProdutos'], [AuthMiddleware::requireAuth(), Rbac::requirePermissions(['ecommerce.update', 'produtos.read'])]);

        // API - Calculadora de Frete
        $router->post('/api/ecommerce/frete/calcular', [$ctrl, 'calcularFrete'], [AuthMiddleware::requireAuth(), Rbac::requirePermissions(['ecommerce.read'])]);

        // API - Categorias
        $router->get('/api/ecommerce/categorias', [$categoriasCtrl, 'list'], [AuthMiddleware::requireAuth(), Rbac::requirePermissions(['ecommerce.read'])]);
        $router->post('/api/ecommerce/categorias', [$categoriasCtrl, 'create'], [AuthMiddleware::requireAuth(), Rbac::requirePermissions(['ecommerce.update'])]);
        $router->put('/api/ecommerce/categorias/{id}', [$categoriasCtrl, 'update'], [AuthMiddleware::requireAuth(), Rbac::requirePermissions(['ecommerce.update'])]);
        $router->delete('/api/ecommerce/categorias/{id}', [$categoriasCtrl, 'delete'], [AuthMiddleware::requireAuth(), Rbac::requirePermissions(['ecommerce.update'])]);
        $router->post('/api/ecommerce/categorias/link-produto', [$categoriasCtrl, 'linkProduto'], [AuthMiddleware::requireAuth(), Rbac::requirePermissions(['ecommerce.update'])]);
        $router->post('/api/ecommerce/categorias/unlink-produto', [$categoriasCtrl, 'unlinkProduto'], [AuthMiddleware::requireAuth(), Rbac::requirePermissions(['ecommerce.update'])]);

        // API - Pedidos (administrativo)
        $pedidosCtrl = new \Modules\Ecommerce\Controllers\EcommercePedidosController();
        $router->get('/api/ecommerce/pedidos', [$pedidosCtrl, 'list'], [AuthMiddleware::requireAuth(), Rbac::requirePermissions(['ecommerce.read'])]);
        $router->get('/api/ecommerce/pedidos/{id}', [$pedidosCtrl, 'getById'], [AuthMiddleware::requireAuth(), Rbac::requirePermissions(['ecommerce.read'])]);
        $router->put('/api/ecommerce/pedidos/{id}/status', [$pedidosCtrl, 'updateStatus'], [AuthMiddleware::requireAuth(), Rbac::requirePermissions(['ecommerce.update'])]);

        // Rotas Públicas do E-commerce (sem autenticação)
        // Estas rotas são acessadas pelos consumidores finais
        $router->get('/loja', [$publicCtrl, 'viewHome'], []);
        $router->get('/loja/produto/{id}', [$publicCtrl, 'viewProduto'], []);
        $router->get('/loja/categoria/{slug}', [$publicCtrl, 'viewCategoria'], []);
        $router->get('/loja/carrinho', [$publicCtrl, 'viewCarrinho'], []);
        $router->get('/loja/checkout', [$publicCtrl, 'viewCheckout'], []);
        
        // API Pública
        $router->get('/api/public/ecommerce/produtos', [$publicCtrl, 'listProdutos'], []);
        $router->get('/api/public/ecommerce/produtos/{id}', [$publicCtrl, 'getProduto'], []);
        $router->get('/api/public/ecommerce/categorias', [$publicCtrl, 'listCategorias'], []);
        $router->post('/api/public/ecommerce/frete/calcular', [$publicCtrl, 'calcularFrete'], []);
        $router->post('/api/public/ecommerce/pedido', [$publicCtrl, 'criarPedido'], []);
        $router->post('/api/public/ecommerce/pagamento/webhook', [$publicCtrl, 'webhookPagamento'], []);
    }

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

