<?php

namespace Modules\SPED;

use Gestoreasy\Core\Module\ModuleInterface;
use Gestoreasy\Core\Routing\Router;
use Gestoreasy\Core\Http\AuthMiddleware;
use Gestoreasy\Core\Auth\Rbac;
use Modules\SPED\Controllers\SPEDFiscalController;
use Modules\SPED\Controllers\SPEDContribuicoesController;
use Modules\SPED\Controllers\SPEDContabilController;
use Modules\SPED\Controllers\SPEDViewsController;

class Module implements ModuleInterface {
    public function register(Router $router): void {
        $fiscalCtrl = new SPEDFiscalController();
        $contribCtrl = new SPEDContribuicoesController();
        $contabilCtrl = new SPEDContabilController();
        $viewsCtrl = new SPEDViewsController();

        // Views
        $router->get('/sped', [$viewsCtrl, 'dashboard'], [AuthMiddleware::requireAuthHtml(), Rbac::requirePermissions(['fiscal.sped.read'])]);
        $router->get('/sped/fiscal', [$viewsCtrl, 'fiscal'], [AuthMiddleware::requireAuthHtml(), Rbac::requirePermissions(['fiscal.sped.read'])]);
        $router->get('/sped/contribuicoes', [$viewsCtrl, 'contribuicoes'], [AuthMiddleware::requireAuthHtml(), Rbac::requirePermissions(['fiscal.sped.read'])]);
        $router->get('/sped/contabil', [$viewsCtrl, 'contabil'], [AuthMiddleware::requireAuthHtml(), Rbac::requirePermissions(['fiscal.sped.read'])]);

        // API - SPED Fiscal (EFD-ICMS/IPI)
        $router->post('/api/sped/fiscal/gerar', [$fiscalCtrl, 'gerar'], [AuthMiddleware::requireAuth(), Rbac::requirePermissions(['fiscal.sped.read'])]);
        $router->post('/api/sped/fiscal/validar', [$fiscalCtrl, 'validar'], [AuthMiddleware::requireAuth(), Rbac::requirePermissions(['fiscal.sped.read'])]);
        $router->get('/api/sped/fiscal/arquivos', [$fiscalCtrl, 'listArquivos'], [AuthMiddleware::requireAuth(), Rbac::requirePermissions(['fiscal.sped.read'])]);
        $router->post('/api/sped/fiscal/enviar', [$fiscalCtrl, 'enviar'], [AuthMiddleware::requireAuth(), Rbac::requirePermissions(['fiscal.sped.manage'])]);

        // API - SPED Contribuições (EFD-Contribuições)
        $router->post('/api/sped/contribuicoes/gerar', [$contribCtrl, 'gerar'], [AuthMiddleware::requireAuth(), Rbac::requirePermissions(['fiscal.sped.read'])]);
        $router->post('/api/sped/contribuicoes/validar', [$contribCtrl, 'validar'], [AuthMiddleware::requireAuth(), Rbac::requirePermissions(['fiscal.sped.read'])]);
        $router->get('/api/sped/contribuicoes/arquivos', [$contribCtrl, 'listArquivos'], [AuthMiddleware::requireAuth(), Rbac::requirePermissions(['fiscal.sped.read'])]);

        // API - SPED Contábil (ECD)
        $router->post('/api/sped/contabil/gerar', [$contabilCtrl, 'gerar'], [AuthMiddleware::requireAuth(), Rbac::requirePermissions(['fiscal.sped.read'])]);
        $router->post('/api/sped/contabil/validar', [$contabilCtrl, 'validar'], [AuthMiddleware::requireAuth(), Rbac::requirePermissions(['fiscal.sped.read'])]);
        $router->get('/api/sped/contabil/arquivos', [$contabilCtrl, 'listArquivos'], [AuthMiddleware::requireAuth(), Rbac::requirePermissions(['fiscal.sped.read'])]);

        // API - Configurações e Automação
        $router->get('/api/sped/config', [$viewsCtrl, 'getConfig'], [AuthMiddleware::requireAuth(), Rbac::requirePermissions(['fiscal.sped.read'])]);
        $router->post('/api/sped/config', [$viewsCtrl, 'updateConfig'], [AuthMiddleware::requireAuth(), Rbac::requirePermissions(['fiscal.sped.manage'])]);
    }
}

